简体   繁体   English

Javascript-将参数函数传递给异步onchange事件

[英]Javascript - Pass param function to async onchange event

Good evening, I have a problem with the construction of form in Javascript. 晚上好,我对用Javascript构建表单有疑问。 For avoid the repetition of code I'd like to use the same function: 为了避免重复代码,我想使用相同的功能:

function addCheckBoxItem(p, name, value) {

    // add checkbox
    var newCheckbox = document.createElement("input");
    newCheckbox.type = "checkbox";
    newCheckbox.setAttribute("value", p);
    newCheckbox.setAttribute("id", p);
    newCheckbox.setAttribute("name", name);

     newCheckbox.onchange = function(){ 

            var cbs = document.getElementsByName(name);
            for (var i = 0; i < cbs.length; i++) {
                cbs[i].checked = false;
            }
            this.checked = true;
            // define the peer selected as choice
            value = this.value;
           // TODO: MY PROBLEM IS HERE
    };

   form.appendChild(newCheckbox);


    // add label to checkbox
    var label = document.createElement('label');
    label.setAttribute("name", name);
    label.htmlFor = p;
    label.appendChild(document.createTextNode(p));

    form.appendChild(label);

    // add br tag to checkbox 
    var br = document.createElement("br")
    br.setAttribute("name", name);
    form.appendChild(br);

}

I call my function inside loop 我在循环内调用函数

for (i = 0; i < array.length; i++) { 
        addCheckBoxItem(array[i].key, nameItemCheckbox, peer_choice);
    }

I know that value = this.value isn't possible because onchange event is Async but i want retrieve the value of this event and associate it with my param. 我知道value = this.value是不可能的,因为onchange事件是异步的,但我想检索此事件的值并将其与我的参数关联。 I have find different response to this problem. 我对这个问题有不同的反应。 One of this is: Why is my variable unaltered after I modify it inside of a function? 其中之一是: 为什么在函数内部修改变量后,变量却没有改变? - Asynchronous code reference I have understand the basic concept but none of the example is relevant to my problem. -异步代码参考我已经理解了基本概念,但是没有一个示例与我的问题有关。 Someone could help me? 有人可以帮我吗?

EDIT - SOLUTION 编辑-解决方案

Thanks to @Tibrogargan i have modified my function in this way: 感谢@Tibrogargan,我以这种方式修改了我的功能:

for (i = 0; i < array.length; i++) {
        addCheckBoxItem(form, array[i].key, form_peer_available_class, peer_choice, (value) => peer_choice = value);
    }

and

function addCheckBoxItem(p, name, value, callback) {

// add checkbox
var newCheckbox = document.createElement("input");
newCheckbox.type = "checkbox";
newCheckbox.setAttribute("value", p);
newCheckbox.setAttribute("id", p);
newCheckbox.setAttribute("name", name);

 newCheckbox.onchange = function(){ 

        var cbs = document.getElementsByName(name);
        for (var i = 0; i < cbs.length; i++) {
            cbs[i].checked = false;
        }
        this.checked = true;
        // define the peer selected as choice
        callback(this.value);
};

form.appendChild(newCheckbox);


// add label to checkbox
var label = document.createElement('label');
label.setAttribute("name", name);
label.htmlFor = p;
label.appendChild(document.createTextNode(p));

form.appendChild(label);

// add br tag to checkbox 
var br = document.createElement("br")
br.setAttribute("name", name);
form.appendChild(br);

}

Pass callback into addCheckBoxItem like that 像这样将回调传递到addCheckBoxItem

// accumulator
var data = {}
// callback
var storeData = (name, value, p) => data[name] = value;
// callback as third argument
addCheckBoxItem('data1', 'checkbox1', storeData);
addCheckBoxItem('data2', 'checkbox2', storeData);
addCheckBoxItem('data3', 'checkbox3', storeData);

Check full example here http://jsfiddle.net/gtqnc109/9/ 在此处查看完整示例http://jsfiddle.net/gtqnc109/9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM