将参数传递给jQuery中的函数

时间:2016-04-28 07:02:34

标签: jquery

我试图从动态生成的文本框中传递值,并且还会动态生成其ID。我从该文本框中获取了值,但我不知道如何将其值作为参数传递给函数。有人可以帮我吗。这就是我尝试的方式:

var a = 0;
var b = 0;
var table;
var invoice_number;
//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_'+a+'").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);
        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');
    alert(tr);
    var data = tr.first().text();
    alert(data);
    data.invoice_number = $("#" + txt.id).val();
}

当我这样做时,我得到错误

  

无法识别的表达:#invoice_number _&#39; + a +&#39;)

如何传递值?

2 个答案:

答案 0 :(得分:0)

您调用函数

时出现语法错误
onclick="jQuery(this).save(' + row[3] + ', ' + row[4] + ', ' + jQuery("#invoice_number_'+a+'").val() + ');"

这应该是

onclick="jQuery(this).save(row[3], row[4], jQuery('#invoice_number_' + a).val())"

答案 1 :(得分:0)

这是不正确的:jQuery("#invoice_number_'+a+'").val()

尝试jQuery("#invoice_number_" + a).val()

然后,您应该在alert(value);

上看到一个值