我试图通过AJAX使用jQuery序列化来发送一个表单的部分内容。表单有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>');
});
答案 0 :(得分:30)
如果你真的想只留下一个表格,可以试试像我在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()
答案 1 :(得分:2)
示例,根据您的需要进行修改:
<form name="test">
<input type="textinput" name="first" value="test1" class="form2" /> <br/>
<select name="second" class="form1">
<option value="1">opt 1</option>
<option selected="selected" value="2">opt 2</option>
</select>
<input type="textinput" name="third" value="test1" class="form2" /> <br/>
</form>
<script>
(function() {
// get second form elements
var options = $('form[name=test]').find('input, textarea, select').filter('.form2').serialize();
alert(options);
}())
</script>
这将获得具有form2类的所有输入。
答案 2 :(得分:0)
var formData = $(form).find('#selectedOptions : input') . serialize();
$.post(url, formData) .done(function (data)
{
alert('hi');
});
where #selectedOptions is id of the element.
答案 3 :(得分:0)
更加语义和细化的方法是使用自定义属性来选择不同的表单部分。使表单元素重新分配到不同的按钮更容易,而不会弄乱容器。
$('button[name="button1"]').click(function() {
data = $('[scope="part1"]').serialize();
console.log(data);
});
$('button[name="button2"]').click(function() {
data = $('[scope="part2"]').serialize();
console.log(data);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input type="text" name="text1" scope="part1" />
<input type="text" name="text2" scope="part1" />
<input type="text" name="text3" scope="part1" />
<input type="text" name="text4" scope="part1" />
<button type="button" name="button1">Button 1</button>
<input type="text" name="text5" scope="part2" />
<input type="text" name="text6" scope="part2" />
<input type="text" name="text7" scope="part2" />
<input type="text" name="text8" scope="part2" />
<button type="button" name="button2">Button 2</button>
</form>