[英]Access jquery variable from the code behind
這是我的html:
<input type="hidden" id="HiddenIndex" name="HiddenIndex" runat="server" />
一些標簽和文本框
<asp:Button runat="server" ID="btnGetCoordinates"
Text="Get Coordinates" OnClick="btnGetCoordinates_Click" />
<asp:Label ID="info" runat="server" Text="Waiting...." />
因此,當單擊獲取坐標按鈕時,它將調用Web服務從后面的代碼返回一些json結果。 對話框將彈出帶有這些結果的列表框。 到現在為止,它都可以正常工作。 我的目標是當客戶在列表中選擇一個項目並單擊“選擇”按鈕時,它將返回所選項目的索引,存儲在隱藏字段中,並稍后從后面的代碼中進行操作。 這是我的jQuery函數
function ShowPopup()
{
$("#parentForm").fadeTo(500, .2);
$("#C1Dialog1").dialog({
open: function () {
$(".ui-dialog-titlebar-close").hide();
},
buttons: [{
text: "Select",
click: function () {
var value = " ";
storedIndex = " ";
var selected = $("[id*=lstCandidates] option:selected");
selected.each(function () {
value = $(this).val();
storedIndex = $(this).index();
$("#HiddenIndex").val(storedIndex);
});
alert(value + " and index is " + storedIndex); //Show value and index
alert("html hidden value " + $("#HiddenIndex").val()); //show value
$(this).dialog("close");
$("#parentForm").fadeTo(500, 1);
},
style: "margin-right: 40px;"
},
{
text: "Cancel",
click: function () {
$(this).dialog("close");
$("#parentForm").fadeTo(500, 1);
},
style: "margin-left:0px;"
}]
});
}
</script>
如您所見,警報顯示隱藏字段的值。這是我的代碼
protected void btnGetCoordinates_Click(object sender, EventArgs e)
{
//Show the Dialog
if (count > 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "Popup", "ShowPopup();", true);
// /請不要在這里停止--問題在這里**,“隱藏字段”沒有任何值**
var indexValue = Request.Form["HiddenIndex"];
info.Text = "HiddenIndex is " + indexValue;
}
}
單擊“對話框”的選擇按鈕時,“信息”標簽不顯示任何內容。非常感謝您的幫助。
客戶端ID可能有問題。 發出HTML時,ASP.NET不一定會在標記中使用客戶端ID。
有幾種方法可以解決此問題,但最簡單的方法是使隱藏字段成為純HTML控件。 這樣,ASP.NET就不會再胡鬧了。 所以改變這個
<input type="hidden" id="HiddenIndex" name="HiddenIndex" runat="server" />
對此
<input type="hidden" id="HiddenIndex" name="HiddenIndex"/>
其他選項:
id$='HiddenIndex'
,使其忽略由ASP.NET添加的任何前綴 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.