如何在点击按钮的同时获取最近的文本框的值?实际上,我正在创建一个包含五列的表,其中两列将具有动态生成的文本框,每个列将具有单独的ID。最后一列将包含保存按钮。单击该按钮时,这些值应保存在db中。我试过这样的事:
var a = 0;
var b = 0;
var table;
//var c = 0;
jQuery(document).ready(function(){
jQuery.ajaxSetup({ cache: false });
table = jQuery("#table_billing").dataTable({
"sAjaxSource": "includes/inc-billing2-db.php?mode=billing_dataTable",
"bDestroy": true,
"bPaginate": false,
"bInfo": false,
"bFilter": false,
"bSort": false,
"aoColumnDefs": [
{
"aTargets": [2],
"mRender": function(data, type, row) {
return '<input type="text" class="form-control invoice_number" name="invoice_number" id="invoice_number_'+ a +'" placeholder="Invoice Number" required="required" onblur="getvalue(this)">';
var x = row[3];
var y = row[4];
}
},
{
"aTargets": [3],
"mRender": function(data, type, row) {
return '<input type="text" class="form-control date" name="invoice_date" id="invoice_date_'+ b +'" placeholder="Invoice Date" required="required">';
}
},
{
"aTargets": [4],
"mRender": function(data, type, row) {
return '<input type="button" class="btn-group btn-default btn-sm save" value="Save" name="save_bill" id="save_bill" onclick="jQuery(this).save(' + row[3] + ', ' + row[4] + ', ' + jQuery("#invoice_number_0").val() + ');">';
}
}
],
"fnCreatedRow": function(nRow, aData, iDisplayIndex, iDisplayIndexFull){
a = a + 1;
b = b + 1;
//c = c + 1;
}
});
jQuery.fn.save = function (id, contract_invoice_request_id, value){
alert(id);
alert(contract_invoice_request_id);
alert(value);
if(jQuery("#invoice_number").val() == ''){
alert("Please enter the invoice number!");
jQuery("#invoice_number").focus();
}
if(jQuery("#invoice_date").val() == ''){
alert("Please select the invoice date!");
jQuery("#invoice_date").focus();
}
jQuery.ajax({
type: "POST",
url: "includes/inc-billing-db.php?mode=save_billing",
data:{ "contract_id": aData[2],
"invoice_number": jQuery("#save_bill").val(),
"invoice_date": jQuery("#invoice_date").val()
},
success: function(data, text){
jQuery.msgbox_success(data.message);
jQuery("#billing_"+this.id).closest('tr').remove();
jQuery("#date_"+this.id).closest('tr').remove();
}
});
}
});
function getvalue(txt){
alert(txt.id);
var tr = $("#"+ txt.id).closest('tr');
console.log(tr);
var data = table.row(tr).data();
alert(data);
data.invoice_number = txt.val();
}
但是,我得到的错误是table.row不是函数。有什么不对,我该怎么办?
答案 0 :(得分:0)
请尝试使用此代码
function getvalue(txt){
var txtValue = $(txt).val();
console.log(txtValue);
alert(txtValue);
var tr = $(txt).closest('tr');
console.log(tr);
// note set correct column number
tr.find("td").eq(0).text(txtValue);
}