style page name modificar con change bootstrap body attribute addattr jquery drop-down-menu

page - establecer el valor desplegable por texto usando jquery



jquery name attribute (11)

Aquí hay un ejemplo simple:

$("#country_id").change(function(){ if(this.value.toString() == ""){ return; } alert("You just changed country to: " + $("#country_id option:selected").text() + " which carried the value for country_id as: " + this.value.toString()); });

Esta pregunta ya tiene una respuesta aquí:

Tengo un menú desplegable como:

<select id="HowYouKnow" > <option value="1">FRIEND</option> <option value="2">GOOGLE</option> <option value="3">AGENT</option></select>

En el menú desplegable anterior, conozco el texto del menú desplegable. ¿Cómo se puede establecer el valor de la lista desplegable en document.ready con el texto usando jquery?


El siguiente código funciona para mí:

jQuery(''[id^=select_] > option'').each(function(){ if (this.text.toLowerCase()==''text''){ jQuery(''[id^=select_]'').val(this.value); } });

jQuery (''[id ^ = select_]'') - Esto le permite seleccionar menú desplegable donde la ID del menú desplegable comienza desde select_

Espero que lo anterior ayude!

Saludos S


Este es un método que funciona basado en el texto de la opción, no en el índice. Solo probado.

var theText = "GOOGLE"; $("#HowYouKnow option:contains(" + theText + ")").attr(''selected'', ''selected'');

O, si hay valores similares (gracias shanabus):

$("#HowYouKnow option").each(function() { if($(this).text() == theText) { $(this).attr(''selected'', ''selected''); } });


Esto se trabajó tanto en Chrome como en Firefox

establecer el valor en el cuadro desplegable.

var given = $("#anotherbox").val(); $("#HowYouKnow").text(given).attr(''value'', given);


Para GOOGLE, GOOGLEDOWN, GOOGLEUP, es decir, tipo de valor similar, puedes probar el código siguiente

$("#HowYouKnow option:contains(''GOOGLE'')").each(function () { if($(this).html()==''GOOGLE''){ $(this).attr(''selected'', ''selected''); } });

De esta forma, el número de repeticiones de bucle se puede reducir y funcionará en todas las situaciones.


Para el uso exacto del partido

$("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr(''selected'', ''selected'');

contiene va a seleccionar el último partido que puede no ser exacto.


prueba esto..

$(element).find("option:contains(" + theText+ ")").attr(''selected'', ''selected'');


$("#HowYouKnow option:eq(XXX)").attr(''selected'', ''selected'');

donde XXX es el índice de la que desea.


$("#HowYouKnow option[value=''" + theText + "'']").attr(''selected'', ''selected''); // added single quotes


$("#HowYouKnow").val("GOOGLE");


var myText = ''GOOGLE''; $(''#HowYouKnow option'').map(function() { if ($(this).text() == myText) return this; }).attr(''selected'', ''selected'');