使用jQuery serialize()和AJAX发送表单的一部分

jal*_*jal 22 ajax jquery serialization

我正在尝试使用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>
Run Code Online (Sandbox Code Playgroud)

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>');

    });
Run Code Online (Sandbox Code Playgroud)

Mar*_*der 30

如果你真的想只留下一个表格,试试像我在这个小提琴里做的那样.

为表单创建子部件.

<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>
Run Code Online (Sandbox Code Playgroud)

然后只需选择部件的所有元素:

$(document).ready(function() {
    $('#button1').on('click', function() {
        alert($('#first input').serialize());
    });

      $('#button2').on('click', function() {
        alert($('#second input').serialize());
    });
});
Run Code Online (Sandbox Code Playgroud)

当然,如果您还有选择框,则必须将它们添加到选择器中.例如:

$('#second input, #second select').serialize()
Run Code Online (Sandbox Code Playgroud)