Pregunta

Lo que tengo es esta object.innerHTML que es la siguiente:

<TABLE >
 <TBODY>

  <TR>
   <TD >
      <IMG id=ctl00_Def_ctl00_ucXXXControl_gvGridName_ctl00_ctl05_imgXYZError src="etc/exclamation.png"> 
   </TD>
   <TD>
      <SELECT id=ctl00_Def_ctl00_ucXXXControl_ctl00_ctl05_rcb123 name=ctl00$Def$ctl00$ucXXXControl$gvGridName$ctl00$ctl05$rcb123>
       <OPTION value=0></OPTION> 
       <OPTION value=1>703</OPTION> 
       <OPTION value=3>704</OPTION> 
       <OPTION value=4>801</OPTION> 
       <OPTION value=5>802</OPTION> (etc)
      </SELECT> 
   </TD>
  </TR>
 </TBODY>
</TABLE>

Necesito saber cómo, a través de JavaScript, labor con la burbuja de texto innerHTML. ¿Cómo voy a conseguir el "selectedIndex" de mi elemento select?

Más detalles dolorosos, si desea que esta parte:

Estoy trabajando con un control RadGrid (de Telerik) y usando la opción de editar 'en línea'. Así esta rejilla contiene filas X, con cada fila que tiene células X. El contenido de las celdas son el problema. Cada célula contiene "cosas". Algunos contienen un elemento simple "de entrada", etc, pero tengo que trabajar con contiene una definición de la tabla, que a su vez contiene 1 fila con 2 células. Una célula tiene una imagen, la otra celda tiene una lista desplegable, es decir, un "seleccionar" elemento.

Mi problema es que he utilizado el conjunto de API de cliente RadGrid para que pueda profundizar en la célula. En este punto que en realidad no ofrecen (que puedo encontrar) cualquier forma de trabajo con el contenido de esa celda ... probablemente porque el contenido puede ser cualquier cosa. Así que tengo que encontrar la manera de trabajo con esta cadena HTML. Todavía nuevo a jQuery y JavaScript ... Realmente sólo quiero echar la cadena como un objeto de tabla y luego ejecutar un selector de jQuery contra ese objeto ... pero JavaScript no funciona muy bien que directamente ... de lo que puedo decir hasta aquí. : (

¿Fue útil?

Solución 2

Aceptar, la idea que tenía justo después de la entrada terminó siendo lo que solía, que es lo que shoebox639 sugirió también. Pongo el código para extraer el ClientId requerida en un método de ayuda:

    //****************************************************************************************
//* Find a requested control's clientID as the first parm passed in from a string 
//*    passed in as the second parm. 
//****************************************************************************************
    function locateClientID(requestedClientID, HTML_StringToSearch) {
    var returnValue = "";

    //If we have something to search for and something to search in, do so.  Null or "" will fail this check.
    if (requestedClientID && HTML_StringToSearch) {

        //Find the starting point of the string starting with "id=", then any number of other letters, numbers,
        //  or underscores, then ending in the specified "requestedClientID" parm value.  We add three to offset
        //  the "id+" part of the search string, which we don't want in the results, but include to locate value.
        var startingPoint = HTML_StringToSearch.search("id=[a-z0-9A-Z_]*" + requestedClientID) + 3;

        //If we found a valid starting point, i.e. NOT -1, then continue processing the string.
        if (startingPoint > -1) {

            //Now that we know where our clientID for the requested control starts in the passed in string,
            //  find the ending " " so we know how much to substr off to pull out the requested client id.
            var endingPoint = HTML_StringToSearch.indexOf(" ", startingPoint);
            //The endingPoint could be -1 if there is no space after the "id" property, but all the examples
            //  I have seen have more attributes after.

            //substr out the clientID and return it to the caller.
            returnValue = HTML_StringToSearch.substr(startingPoint, (endingPoint - startingPoint));
         }
    }

    return returnValue;
}
//*****************************************************************************************

Así que en mi caso me pasaría en el rcb123 como el primer Parm, y la burbuja cadena innerHtml como el segundo valor, y la función devolvería el valor ClientId. Después de conseguir la parte posterior ClientId, acabo de hacer otra llamada al método jQuery usando lo siguiente:

function cv123_Validate(sender, eventArgs) {

    //Get a ref to the radGrid's collection of rows.
    var gvRadGridNameRows = $find("<%= gvRadGridName.ClientID %>").MasterTableView.get_dataItems();

    var innerHTML;
    var foundClientID;
    var errorImage;
    var rcb123;

    //Process every row in the radGrid.
    for (var row = 0; row < gvRadGridNameRows.length; row++){
        //Get the cell in question's innerHTML value.
        innerHTML = gvRadGridNameRows.get_cell("uniqueCellName").innerHTML;

        //Get ref to the 'error image'.
        errorImage = $("#" + locateClientID("imgHUDError", innerHTML));

        //locate the unique clientID of the rcb123 in this row.
        foundClientID = locateClientID("rcb123", innerHTML);

        //Use the found unique clientID with jQuery to get a ref to the dropdown list.
        rcb123 = $("#" + foundClientID)[0];

        //If the dropdown list's selected index is 0 or less AND the control is NOT 
        //  disabled, active the single error message tied to this custom validator
        //  and show the 'error image' next to the control.
        if (rcb123.selectedIndex < 1 && rcb123.isDisabled != true) {
            errorImage.css("height", 12);
            eventArgs.IsValid = false;
        }
        else //Otherwise, hide the error image.
        {
            errorImage.css("height", 0);  
        }
    }
}

Todavía estoy probando varios ejemplos, y en busca de cualquier agujero distintos de los anteriormente, pero para mis propósitos esto funciona bien. He creado la rutina de ayuda porque yo también manipular la imagen de la mancha innerHtml también.

La idea era poner una 'imagen de error' al lado de cada control en la red para una referencia visual a donde estaba el error, pero sólo añadir un mensaje de error al control errorSummary, en lugar de X repite los mensajes de error que me dieron cuando simplemente la incorporación de un validador de campo requerida al lado de la lista desplegable. (Mi grupo BA no le gustó ...)

Espero que esto ayude a alguien.

Otros consejos

$('#ctl00_Def_ctl00_ucXXXControl_ctl00_ctl05_rcb123').val()

le dará el valor de la opción seleccionada.

Usted quiere también poner su ID de valores de clase y entre comillas en el html.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top