开发者

How to set the dropdownlist value?

开发者 https://www.devze.com 2022-12-27 18:28 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号