我有一個簡單的模式jQuery對話框彈出窗口,收集地址信息,我需要傳遞給後面的代碼保存到數據庫後,用戶輸入它。我已經搜索了數百個示例,並且目前處於下面的點,但似乎無論我做什麼,創建的JSON字符串都顯示「undefined」,表示我試圖傳遞的所有字段。我的結論是,我用來在jQuery中構建JSON字符串的方法是錯誤的,但是嘗試了很多種方法來完成此操作後,它們都無法工作。我試圖通過ID使用各種方式訪問字段,但類和如下所示。這裏是代碼片段。任何人都可以看到我出錯的地方嗎?獲取JQuery對話框值來解決問題
<script type="text/javascript">
$(function() {
$('#dialog').dialog({
draggable: true,
resizeable: false,
autoOpen: false,
height: 700,
width: 550,
modal: true,
top: 0,
left: 0,
title: 'Edit Information',
buttons: {
'Save': function() {
$('#ibSaveInfo').click();
},
'Cancel': function() {
$(this).dialog('close');
}
}
}).parent().css('z-index', '1005');
});
$(document).on("click", "#ibSaveInfo", function() {
var inputArray = new Array;
var idx = 0;
inputArray[idx] = 'Address1:' + $("#dialog").find("#txtAddress1").val();
idx++;
inputArray[idx] = 'Address2:' + $("#txtAddress2").val();
idx++;
inputArray[idx] = 'city:' + $("txtcity").val();
etc, etc
var inputArrayList = "{ inputArray: " + JSON.stringify(inputArray) + "}";
$.ajax({
type: "POST",
dataType: "json",
contentType: "application/json",
url: "Address.aspx/SaveInfo",
data: inputArrayList,
success: function (data) {
//debugger;
if (data.d.indexOf("Error") != -1) {
}
else {
$("#ResultLabel").show();
$("#ResultLabel").text("Sum of all the contents is: " + data.d);
}
},
error: function (e, ts, et) {
//debugger;
alert(ts);
}
}); //ajax func end
});
$(document).ready(function() {
$("#ibEdit").click(function (e) {
$('#dialog').dialog('option', 'position', [e.pageX + 10, e.pageY + 10]);
$('#dialog').dialog('open');
});
});
<div id="dialog" style="padding-left: 10px; padding-bottom: 15px;">
<asp:TextBox runat="server" ID="txtAddress1" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtAddress2" ClientIDMode="Static" />
<asp:TextBox runat="server" ID="txtCity" ClientIDMode="Static" />
etc, etc
</div>
只注意到你正在使用asp文本框。我在下面解釋了我的答案。當項目運行時,Asp控件會改變它們的id,如果你不從服務器端訪問值,請確保使用常規文本框。 – MorningDew 2013-03-19 21:56:14