我正在尝试使用 jQuery 的序列化通过 AJAX 发送一份表单的部分内容。该表单有 16 个文本字段。我有 4 个按钮。 button0
发送文本字段 0,1,2,3,button1
发送文本字段 4,5,6,7 等。我该怎么做?
HTML
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Serialize</title>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
</head>
<body>
<form id='miForm' class='miForm' name='miForm' action='env.php' method='POST'>
</form>
</body>
</html>
jQuery:
$(document).ready(function(){
for(i=0;i<16;i++){
$('form').append('Campo de texto '+i+'<input type="text" id="txt'+i+'" value="Campo '+i+'" readonly="yes"/><br>');
}
for(i=0;i<4;i++){
$('form').append('<input type="button" id="butEnv'+i+'" value="Enviar'+i+'"/><br>');
}
$('form').append('<input type="button" id="butGen" value="Enviar Global"/><br>');
});
最佳答案
如果您真的只想只使用一种形式,请尝试像我在 this fiddle 中所做的那样.
为您的表单创建子部分。
<form>
<div id="first">
<input name="tbox1" type="text">
<input name="tbox2" type="text">
<input name="tbox3" type="text">
<input id="button1" type="button" value="button1">
</div>
<div id="second">
<input name="tbox4" type="text">
<input name="tbox5" type="text">
<input name="tbox6" type="text">
<input id="button2" type="button" value="button2">
</div>
</form>
然后只需选择零件的所有元素:
$(document).ready(function() {
$('#button1').on('click', function() {
alert($('#first input').serialize());
});
$('#button2').on('click', function() {
alert($('#second input').serialize());
});
});
当然,如果您还有选择框,则必须将它们添加到选择器中。例如:
$('#second input, #second select').serialize()
关于jquery - 使用 jQuery serialize() 和 AJAX 发送部分表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16130619/