26

jQuery のシリアル化を使用して、AJAX で 1 つのフォームの一部を送信しようとしています。フォームには 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>');

    });
4

4 に答える 4

35

本当に1つのフォームだけにとどまりたい場合は、私がこのフィドルで行ったようなことを試してください。

フォームのサブ パーツを作成します。

<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()
于 2013-04-21T11:48:27.810 に答える
3

デモとコードを試す

例、必要に応じて変更します。

<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 クラスを持つすべての入力が取得されます。

于 2013-04-21T11:45:15.607 に答える
1
var formData    =    $(form).find('#selectedOptions : input') . serialize();
         $.post(url, formData)  .done(function (data) 
         {  
             alert('hi');
        });

where #selectedOptions is id of the element.
于 2016-09-02T08:00:16.807 に答える