简体   繁体   中英

How to set the dropdownlist value?

I am using this technique: is there a way to hold the values? - lost in postback

After it does the postback, how do I set which item is selected?

$(document).ready(function() { 
     if (document.getElementById("txtHidData").value != "")
        $("#country").val(document.getElementById("txtHidData").value);
        //or
        //$("#country")[0].selectedIndex = document.getElementById("txtHidData").value;

Does not work either way, any help? Thanks.

EDIT:

$("#country").change(function() {
         debugger
         var _selected = $("#country option:selected").val();
         document.getElementById("txtHidData").value = "";
         document.getElementById("txtHidData").value = _selected;
         // $("#txtHidData").value = _selected;

....

I don't know exactly what your markup looks like, but here's a shot:

$(document).ready(function () {
    var val = $("#txtHidData").val();
    if (val !== "") {
        $("#country > option[value=" + val + "]").attr("selected", "selected");
    }
    ...
});

Try not to mix jQuery and the native DOM functions if jQuery provides equivalent functions. Doing so defeats the purpose of using jQuery.

Edit: had some incorrect open/close quotes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM