Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将JS对象引用到DOM元素_Javascript_Jquery_Dom - Fatal编程技术网

Javascript 将JS对象引用到DOM元素

Javascript 将JS对象引用到DOM元素,javascript,jquery,dom,Javascript,Jquery,Dom,如何将特定的DOM元素引用到特定的JS对象? 例如,我有一系列客户。使用jQuery,我为每个客户创建LI,并使用复选框和span作为客户名称。单击复选框时,我需要对customer JS对象进行一些处理。问题是,我如何能够以一种简单的方式获得这个JS对象。 目前我有以下几点: $(customers).each(function(){ $("<li>").append($("<input type=\"checkbox\"").attr("id","chk_" + this.

如何将特定的DOM元素引用到特定的JS对象? 例如,我有一系列客户。使用jQuery,我为每个客户创建LI,并使用复选框和span作为客户名称。单击复选框时,我需要对customer JS对象进行一些处理。问题是,我如何能够以一种简单的方式获得这个JS对象。 目前我有以下几点:

$(customers).each(function(){
$("<li>").append($("<input type=\"checkbox\"").attr("id","chk_" + this.ID)).append($("<span>").text(this.Name)).appendTo("#ulCustomers");
});

$("#ulCustomers input[type=checkbox]").bind("click",function(){

var customerId = $(this).attr("id").replace("chk_","");
var CustomerObj = $(customers).filter(function () { return this.ID == customerId }).get(0);

myProcess(CustomerObj); //Two above two lines are just for select correct customer from array.
});
$(客户)。每个(函数(){

$(“
  • ”)。追加($(“您不能将单击事件绑定到引用相关客户对象的闭包吗

    像这样

    $(customers)
    .each(function(){
        var custObj = this;
        $("<li>")
            .append(
                $("<input type=\"checkbox\"")
                .append($("<span>")
                .text(this.Name))
                .appendTo("#ulCustomers")
                .bind("click", function(){
                    myProcess(custObj);
                })
    });
    
    $(客户)
    .each(函数({
    var custObj=此;
    $(“
  • ”) .附加( $(“您可以使用jquery函数

    $(客户)。每个(函数(){
    var elem=$(“
  • ”+this.Name+“
  • ”).appendTo(“#ulCustomers”); 元素查找(“输入”)。数据(“客户”,本); }); $(“#用户输入[类型=复选框]”)。单击(函数(){ var CustomerObj=$(此).data(“客户”); myProcess(CustomerObj); });
    我会使用jQuery数据,如下所示:

    $(“复选框”).data('customer',this.ID)

    要检索数据,请执行以下操作:

    $("#ulCustomers input[type=checkbox]").bind("onchange",function(){
    
    var customerId = $(this).data("customer");
    var CustomerObj = $(customers).filter(function () { return this.ID == customerId }).get(0);
    
    myProcess(CustomerObj); //Two above two lines are just for select correct customer from array.
    });
    

    另外,不要在复选框上使用click event;,使用onchange event;)

    是的,这是最简单的方法,但我试图避免这种类型的闭包:)我认为在数据中存储对整个客户obj的引用将比只存储id更有效。我知道onchange存在,但我建议使用onclick,然后检查是否选中:)
    $("#ulCustomers input[type=checkbox]").bind("onchange",function(){
    
    var customerId = $(this).data("customer");
    var CustomerObj = $(customers).filter(function () { return this.ID == customerId }).get(0);
    
    myProcess(CustomerObj); //Two above two lines are just for select correct customer from array.
    });