ustaw wartość rozwijaną przez tekst za pomocą jquery [duplikat]
To pytanie ma już odpowiedź tutaj:
- Ustawienie select by label poprzez jquery w 1.4 4 odpowiedzi
Mam listę rozwijaną jako:
<select id="HowYouKnow" >
<option value="1">FRIEND</option>
<option value="2">GOOGLE</option>
<option value="3">AGENT</option></select>
W powyższym rozwijaniu znam tekst rozwijanego. Jak ustawić wartość rozwijanej listy w dokumencie.gotowy tekst za pomocą jquery?
11 answers
Jest to metoda, która działa w oparciu o tekst opcji, a nie indeks. Tylko sprawdzony.
var theText = "GOOGLE";
$("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected');
Lub, jeśli są podobne wartości (dzięki shanabus):
$("#HowYouKnow option").each(function() {
if($(this).text() == theText) {
$(this).attr('selected', 'selected');
}
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-06-12 20:35:20
Do dokładnego dopasowania
$("#HowYouKnow option").filter(function(index) { return $(this).text() === "GOOGLE"; }).attr('selected', 'selected');
Contains wybierze ostatni mecz, który może nie być dokładny.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-20 00:35:44
$("#HowYouKnow option[value='" + theText + "']").attr('selected', 'selected'); // added single quotes
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-19 15:15:08
$("#HowYouKnow").val("GOOGLE");
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-11 16:40:59
var myText = 'GOOGLE';
$('#HowYouKnow option').map(function() {
if ($(this).text() == myText) return this;
}).attr('selected', 'selected');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-02-08 16:29:54
Dla GOOGLE, GOOGLEPOWN, GOOGLEPUP i. e podobny rodzaj wartości można spróbować poniżej kodu
$("#HowYouKnow option:contains('GOOGLE')").each(function () {
if($(this).html()=='GOOGLE'){
$(this).attr('selected', 'selected');
}
});
W ten sposób można zmniejszyć liczbę iteracji pętli i będzie działać w każdej sytuacji.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-06-09 11:31:40
Poniższy kod działa dla mnie -:
jQuery('[id^=select_] > option').each(function(){
if (this.text.toLowerCase()=='text'){
jQuery('[id^=select_]').val(this.value);
}
});
JQuery ('[id^=select_]') - pozwala na wybranie listy rozwijanej, gdzie ID Listy zaczyna się od select_
Mam nadzieję, że powyższe pomoże! Pozdrawiam SWarning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-27 18:28:36
Spróbuj tego..
$(element).find("option:contains(" + theText+ ")").attr('selected', 'selected');
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-10-13 07:12:07
Oto prosty przykład:
$("#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());
});
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-02 21:03:46
To działa zarówno chrome jak i firefox
Ustaw wartość w rozwijanym polu.
var given = $("#anotherbox").val();
$("#HowYouKnow").text(given).attr('value', given);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-29 11:05:14
$("#HowYouKnow option:eq(XXX)").attr('selected', 'selected');
Gdzie XXX jest indeksem tego, którego chcesz.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2009-12-11 15:48:14