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>
 473
Author: Peter Mortensen, 2009-09-12

25 answers

$("#target").val($("#target option:first").val());
 807
Author: David Andres,
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);
 116
Author: Esben,
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');
 103
Author: user149513,
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.

 58
Author: Jimmy Huang,
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");
 39
Author: James Lee Baker,
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;
 30
Author: danactive,
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).

 17
Author: Bradley Bossard,
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')
 15
Author: Melanie,
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;

});
 13
Author: Nick Petropoulos,
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)

 7
Author: FFFFFF,
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);
 7
Author: Ramon Schmidt,
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');
 6
Author: Francis Lewis,
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');
 5
Author: Fabrizio,
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.
 4
Author: Darth Jon,
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.
 2
Author: Gary - ITB,
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

Użycie:

$("#selectbox option:first").val()

Proszę znaleźć proste działanie w ten JSFiddle .

 2
Author: Chetan,
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');
 1
Author: Matthew Scott,
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');
 1
Author: Pablo S G Pacheco,
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(); });

 1
Author: Jean Douglas,
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

 0
Author: Krishna Murthy,
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());
 0
Author: Daniel,
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());
 0
Author: GobsYats,
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()); 
});
 0
Author: jdnichollsc,
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());
 0
Author: Muhammad Qasim,
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()); 
 0
Author: Arsman Ahmad,
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