Jak zrobić pierwszą opcję wybraną z jQuery
Jak zrobić pierwszą opcję zaznaczoną za pomocą jQuery?
<select id="target">
<option value="1">...</option>
<option value="2">...</option>
</select>
25 answers
$("#target").val($("#target option:first").val());
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-09-12 04:31:42
Możesz spróbować tego
$("#target").prop("selectedIndex", 0);
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-08-08 09:09:51
// remove "selected" from any options that might already be selected
$('#target option[selected="selected"]').each(
function() {
$(this).removeAttr('selected');
}
);
// mark the first option as selected
$("#target option:first").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
2011-12-08 21:28:22
Kiedy używasz
$("#target").val($("#target option:first").val());
To nie będzie działać w Chrome i Safari, Jeśli pierwsza wartość opcji to null.
Wolę
$("#target option:first").attr('selected','selected');
Ponieważ może działać we wszystkich przeglądarkach.
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
2018-01-08 14:13:02
Zmiana wartości wejścia select lub dostosowanie wybranego atrybutu może nadpisać domyślną właściwość selectedOptions elementu DOM, powodując, że element może nie resetować się poprawnie w formie, w której zostało wywołane zdarzenie reset.
Użyj metody jQuery prop, aby wyczyścić i ustawić wymaganą opcję:
$("#target option:selected").prop("selected", false);
$("#target option:first").prop("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
2013-05-16 23:12:34
$("#target")[0].selectedIndex = 0;
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-05-24 13:37:26
Jeden subtelny punkt i myślę odkryłem o najlepszych głosowanych odpowiedzi jest to, że nawet jeśli poprawnie zmienić wybraną wartość, nie aktualizują elementu, który użytkownik widzi (tylko po kliknięciu widżetu zobaczą czek obok zaktualizowanego elementu).
Łańcuchy a .wywołanie change () do końca również zaktualizuje widżet interfejsu użytkownika.
$("#target").val($("#target option:first").val()).change();
(zauważ, że zauważyłem to podczas korzystania z jQuery Mobile i box na pulpicie Chrome, więc może to nie być case everywhere).
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
2015-12-08 22:15:54
Jeśli masz wyłączone opcje, możesz dodać not ([disabled]) , aby zapobiec ich wybraniu, co powoduje, że:
$("#target option:not([disabled]):first").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
2018-01-08 14:13:32
Innym sposobem resetowania wartości (dla wielu wybranych elementów) może być:
$("selector").each(function(){
/*Perform any check and validation if needed for each item */
/*Use "this" to handle the element in javascript or "$(this)" to handle the element with jquery */
this.selectedIndex=0;
});
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
2018-08-14 07:16:33
$('#newType option:first').prop('selected', true)
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-08-19 03:46:43
Takie proste:
$('#target option:first').prop('selected', true);
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-02-19 09:27:12
Odkryłem, że samo ustawienie attr selected nie działa, jeśli istnieje już wybrany atrybut. Kod, którego teraz używam, najpierw usunie wybrany atrybut, a następnie wybierze pierwszą opcję.
$('#target').removeAttr('selected').find('option:first').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
2011-09-16 06:02:10
Oto jak bym to zrobił:
$("#target option")
.removeAttr('selected')
.find(':first') // You can also use .find('[value=MyVal]')
.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
2018-01-08 14:14:01
Chociaż każda funkcja prawdopodobnie nie jest konieczna ...
$('select').each(function(){
$(this).find('option:first').prop('selected', 'selected');
});
Mi pasuje.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-10-24 13:23:25
Jeśli masz zamiar użyć pierwszej opcji jako domyślnej jak
<select>
<option value="">Please select an option below</option>
...
Wtedy możesz po prostu użyć:
$('select').val('');
To miłe i proste.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
2018-01-08 14:14:35
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
2018-01-08 14:15:21
Na odwrocie odpowiedzi Jamesa Lee Bakera, wolę to rozwiązanie, ponieważ usuwa poleganie na obsłudze przeglądarki dla :first: selected ...
$('#target').children().prop('selected', false);
$($('#target').children()[0]).prop('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
2016-10-18 15:30:43
U mnie zadziałało tylko użycie wyzwalacza ('change') na końcu, jak to:
$("#target option:first").attr('selected','selected').trigger('change');
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-11-10 21:19:08
Dla mnie zadziałało tylko wtedy, gdy dodałem następujący kod:
.change();
Dla mnie zadziałało tylko wtedy, gdy dodałem następujący kod: Ponieważ chciałem "zresetować" formularz, czyli wybrać wszystkie pierwsze opcje ze wszystkich opcji formularza, użyłem następującego kodu:
$('form').find('select').each(function(){
$(this).val($("select option:first").val());
$(this).change();
});
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-09-13 11:31:29
$("#target").val(null);
Pracował dobrze w chromie
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-09-15 07:55:14
Jeśli przechowujesz obiekt jQuery elementu select:
var jQuerySelectObject = $("...");
...
jQuerySelectObject.val(jQuerySelectObject.children().eq(0).val());
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-11-26 22:14:53
Użycie:
alert($( "#target option:first" ).text());
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
2018-01-08 14:15:38
Sprawdź to najlepsze podejście używając jQuery z ECMAScript 6:
$('select').each((i, item) => {
var $item = $(item);
$item.val($item.find('option:first').val());
});
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
2018-01-08 14:17:58
Dla mnie zadziałało tylko wtedy, gdy dodałem następujący wiersz kodu
$('#target').val($('#target').find("option:first").val());
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
2018-06-11 07:46:42
Możesz wybrać dowolną opcję z dropdown
używając jej.
// 'N' can by any number of option. // e.g., N=1 for first option
$("#DropDownId").val($("#DropDownId option:eq(N-1)").val());
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
2018-07-04 07:02:34