在用户更改触发更新面板回发的选择元素后,我试图在回发完成后将相同选择的值设置为所选值,但不知何故我在隐藏字段中设置的值的值回发后丢失。
function countrypostback() {
$('#countryid').val($('select[name="countryselect"]').val());
__doPostBack('upnlSearch', '');
}
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/service.svc/countries/",
data: "",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
for (var i in msg) {
var resultitems = '<select onchange="countrypostback();" class="textbox" name="countryselect" id="countryselect">';
for (var i in msg) {
if (msg[i].name != '') {
resultitems += '<option value="' + msg[i].value + '">' + msg[i].name + '</option>';
}
}
resultitems += '</select>';
$('#countryselect').html(resultitems);
}
}
});
//here I'm trying to set the value of the dropdown to the value that was selected before the postback
$("#countryselect").val($('#countryid').val());
<input type="hidden" id="countryid" name="countryid" />
<asp:UpdatePanel ID="upnlSearch" runat="server">
<ContentTemplate>
<span id="countryselect"></span>
</ContentTemplate>
</asp:UpdatePanel>
我尝试将隐藏字段 countryid 放置在更新面板的外部和内部,但这并没有什么不同,当我尝试通过它访问它时,我仍然有一个空的表单域
$('#countryid').val()
我宁愿不使用视图状态(如果可能的话),因为这会增加页面加载。