2017-08-02 60 views
0

我有一個函數返回一行的值,但我不能將它們分開,並將它們放入不同的參數中。如何獲得一行的數據,如果複選框被選中

function guardarImpuestos() { 
    $('input[type=checkbox]').each(function() { 
    if ($(this).is(':checked')) { 
     //test 
     console.log($(this).closest("tr").children("td").get()[0]); 
     console.log($(this).closest("tr").children("td").get()[1]); 
     console.log($(this).closest("tr").children("td").get()[2]); 
     //end test 
     var $row = $(this).closest('tr'); 
     $('td', $row).each(function(i) { 
     var abc = $(this).text(); 
     console.log(abc); 
     }) 
     var parametros = { 
     "cuit": $("#cuit").val(), 
     "impuesto": i need the first val here, 
     "concepto": and second here 
     }; 
     $.ajax({ 
     data: parametros, 
     url: 'api/insertar_impuestos.php', 
     type: 'post', 
     success: function(response) { 
      alert("Impuesto agregado."); 
     } 
     }); 
    } 
    }); 
} 

回答

1

你也可以遍歷該行中的每個元素<td>,並把從每一個文本到一個數組。然後,你可以使用數組符號values[0]

function guardarImpuestos() { 

$('input[type=checkbox]:checked').each(function() { 

    var $row = $(this).closest('tr'); 
    var values = []; 

    $('td', $row).each(function(i){ 
     values.push($(this).text()); 
    }) 
    var parametros = { 

    "cuit" : $("#cuit").val(), 
    "impuesto" : values[0], 
    "concepto" : values[1] 
    }; 

    $.ajax({ 

     data: parametros, 
     url: 'api/insertar_impuestos.php', 
     type: 'post', 
     success: function (response) { 
       alert("Impuesto agregado."); 
     } 

    }); 

}); 

} 

的另一種方法去做訪問這些文本元素,如果你有少量細胞,確定的數量,是跳過$('td',$row).each(功能,只是這樣做:

... 
var parametros = { 
    "cuit" : $("#cuit").val(), 
    "impuesto": $('td',$row).eq(0).text(), 
    "concepto": $('td',$row).eq(1).text() 
} 
... 
+0

完善,解決 –

+0

你能解釋一下什麼是錯的,你是如何解決它,所以大家不必做一個代碼比較瞭解? – Barmar

+0

@Barmar良好的通話,更新。謝謝。 – JakeParis

相關問題