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.
精彩评论