How to set the 'selected option' of a select dropdown list with jquery

后端 未结 6 1500
南笙
南笙 2021-01-31 09:07

I have the following jquery function:

$.post(\'GetSalesRepfromCustomer\', {
    data: selectedObj.value
}, function (result) {
    alert(result[0]);
    $(\'sele         


        
6条回答
  •  孤独总比滥情好
    2021-01-31 09:50

    One thing I don't think anyone has mentioned, and a stupid mistake I've made in the past (especially when dynamically populating selects). jQuery's .val() won't work for a select input if there isn't an option with a value that matches the value supplied.

    Here's a fiddle explaining -> http://jsfiddle.net/go164zmt/

    
    
    $("#example").val("0");
    alert($("#example").val());
    $("#example").val("1");
    alert($("#example").val());
    
    //doesn't exist
    $("#example").val("2");
    //and thus returns null
    alert($("#example").val());
    

提交回复
热议问题