1

チェックボックスがチェックされている場合、2行ごとにすべての要素を取得し、それらをjson配列にして、ajaxでphpに送信したい.

$('input.pay').each(function(){
    if($(this).is(':checked'))
    {
        row = $(this).parents('tr').attr('id').split('_');
        type = row[0];
        payID= row[1];
        payJsonArr = '';
        secondRow=', #'+type+'_'+payID+$('#rate_'+payID).attr('class');

        $('#'+type+'_'+payID+secondRow).find('.take').each(function(){
            if($(this).is('input') || $(this).is('select'))
                payJsonArr += $(this).attr('name') + ':' + $(this).val()+',';   
            else if($(this).is('td') || $(this).is('span'))
                payJsonArr += $(this).attr('name') +':'+ $(this).html().trim()+',';
        });
        payJsonArr += payJsonArr.substring(0, payJsonArr.length - 1);
        payments[payID]= '{'+payJsonArr+'}';
    }
});

問題は、そのコードを使用して、php で配列を取得すると、次のようになることです。

array(1) {
  [791]=>
  string(501) "{field 1:2012-10-07,field 2:6777.00 }"
}

そのようなJSONの場合、どうすればそれを取得できますか:

array(1) {
  [791]=>
    [field 1]=>'2012-10-07',
    [field 2]=>'6777.00'
}

困っている人を助けてくれてありがとう。

4

3 に答える 3

2

そんな使い方もできますが、

    `var payJsonArr = [];
    $('#'+type+'_'+payID+secondRow).find('.take').each(function(){
        if($(this).is('input') || $(this).is('select'))
            payJsonArr[$(this).attr('name')] = $(this).val();   
        else if($(this).is('td') || $(this).is('span'))
            payJsonArr[$(this).attr('name')] = $(this).html().trim();
    });

    payment[payID]= payJsonArr;`

JSON について詳しく知りたい場合は、ここでいくつかの良い例を見ることができます: JSON

于 2012-10-11T18:13:31.683 に答える
1

Rohitが提案したことに基づいて、これを試してください(これはテストされていません):

$('input.pay').each(function(){
if($(this).is(':checked'))
{
    row = $(this).parents('tr').attr('id').split('_');
    type = row[0];
    payID= row[1];
    payJsonArr = {};
    secondRow=', #'+type+'_'+payID+$('#rate_'+payID).attr('class');

    $('#'+type+'_'+payID+secondRow).find('.take').each(function(){
        if($(this).is('input') || $(this).is('select'))
            payJsonArr[$(this).attr('name')] = $(this).val();   
        else if($(this).is('td') || $(this).is('span'))
            payJsonArr[$(this).attr('name')] = $(this).html().trim();
    });
    payments[payID]= payJsonArr;
}

});

于 2012-10-11T16:53:32.980 に答える
0

JSON に変換できる文字列を作成してみてください。このために、次のような文字列を作成します。

var jsonString= {
             "field1":"value",
             "field2":"value" 

           };

二重引用符を使用している場合は常に二重引用符のみを使用し、単一引用符を使用する場合は常に単一引用符を使用することに注意してください。

于 2012-10-11T16:08:58.307 に答える