我试图看看如何通过序列化传递动态输入类型,并在我的ajax php页面中处理以通过json返回。如您所见,它由for循环组成,它根据滑块递增每个输入。我不能只处理id =“BRV-brtrv-boxamount-因为它不认识它。如果有人可以帮助我,我将不胜感激。谢谢
for(var i = 0;i < $(this).val();i++) {
$("#BRV-brtrv-boxnumber").append('<div data-role="fieldcontain"><label for="BRV-brtrv-boxamount" class="ui-input-text">Enter box ' + (i + 1) + ' number:</label><input type="text" name="BRV-brtrv-boxamount-' + i + '-no" id="BRV-brtrv-boxamount-' + i + '-no" class="BRV-brtrv-boxamount ui-input-text ui-body-null ui-corner-all ui-shadow-inset ui-body-a" required="required" /></div>')
}
+++ +++ EDIT
$(function() {
$("#BRV_brtrv").submit(function() {
var send = $(this).serialize();
$.ajax({
type: "POST",
url: "boxrtrvajax.php",
cache: false,
data: send,
dataType: "json",
success: function (msg) {
$('#brtv-result').addClass("result_msg").html(msg.service);
},
error:function (xhr, ajaxOptions, thrownError){
jAlert('There was an exception thrown somewhere');
alert(xhr.status);
alert(thrownError);
}
});
return false;
});
});
答案 0 :(得分:1)
var url = 'my.ajax.php';
var params = new Object();
$('input, select').each(function () {
params[this.name] = this.value;
});
$.post(url, params);
既然你提供了一个完整的例子,这里就是完整的答案:
$(function() {
$("#BRV_brtrv").submit(function() {
var send = $(this).serialize();
$.getJSON("boxrtrvajax.php", send, function(msg) {
$('#brtv-result').addClass("result_msg").html(msg.service);
})
.error(function (xhr, ajaxOptions, thrownError) {
jAlert('There was an exception thrown somewhere');
alert(xhr.status);
alert(thrownError);
});
return false;
});
});
但你的例子也应该有效。注意:您可以使用$ .post()而不是$ .getJson()(甚至是$ .get(),因为$ .get和$ .post都能识别并解析json响应(如果有的话)。