Jquery ajax POST标签/值对

时间:2012-09-13 21:36:58

标签: php jquery

我想在Ajax .POST中传递标签/值对,但找不到任何解决方案。 Ajax .POST通常发送id / value对但我无法更改我的id并希望发送标签/值对。我提供了部分表格。请帮忙。

        $.ajax({
            url:"allfields.php",
            type:"POST",
    //      dataType:"json",
            data: $("#frmRequest").serialize(),
            success: function(msg){
                alert("Form Submitted: "+ msg);
                return msg;

              },
             error: function() {
            alert('Error occured');
             }
        });

<body>
     <form id="frmRequest" name="frmRequest" >

                <div class="clearfix" id="idRequestDetails"  >
                    <table width="809" border="0" id="tbl_data_1_1_1_1__" summary="Profile">
                      <tr>
                        <th width="156" scope="col"><label class="labelrequest" for="txtProfileName1__">Name</label>
                        </th>
                        <th width="74" scope="col"><label class="labelrequest" for="txtProfileUserID1__">User ID</label></th>
                        <th width="131" scope="col"><label class="labelrequest" for="txtSiteCost1__">Site Cost Centre</label></th>
                         <th width="182" scope="col"><label class="labelrequest" for="txtDetail1__">Additional Details</label></th>
                      </tr>
                      <tr>
                        <td><input type="text" name="txtProfileName1__" id="txtProfileName1__" tabindex="100" /></td>
                        <td><input name="txtProfileUserID1__" type="text" class="clearfix" id="txtProfileUserID1__" tabindex="110" size="8" /></td>
                        <td><input name="txtSiteCost1__" type="text" id="txtSiteCost1__" tabindex="220" size="8" /></td>

                        <td><textarea name="txtDetail1__" rows="1" id="txtDetail1__" tabindex="240"></textarea></td>
                      </tr>
                    </table>
                  </div>
        </body>

1 个答案:

答案 0 :(得分:4)

以下是一个例子:

HTML

<form>
    <label>Label1</label><input type="text" value="1">
    <label>Label2</label><input type="text" value="some">
</form>

的jQuery

var dataToServer = [];
$('form label').each(function() {
    dataToServer.push({
        label: $(this).text(),
        value: $(this).next().val()        
    });
});

console.log(data);
// output
// [ {label: 'Label1', value: '1'}, {label: 'Label2', value: 'some'}]

Working Example

希望这可以为你提供一些指导。

根据commnet

AJAX部分

$.ajax({
    url: "allfields.php",
    type: "POST",
    dataType:"json",
    data: dataToServer,   // send above data here
    success: function(msg) {
        alert("Form Submitted: " + msg);
        return msg;
    },
    error: function() {
        alert('Error occured');
    }
});

最后一击

$('#submit').on('click', function() {
    var dataToServer = [];
    $('#frmRequest label').each(function(i, el) {
        var temp= {},
            label = $(this).text(),
            value = $('#' + $(this).attr('for')).val();
        temp[label] = value;
        dataToServer.push(temp);
    });
    console.log(dataToServer);
});

See the console output