繁体   English   中英

无法通过JavaScript中动态创建的复选框的检查操作来创建表格行

[英]Not able to create a table row from the check action of a dynamically created checkbox in javascript

我试图在动态创建的复选框的检查操作上创建表的行。 流程未在检查事件上进入功能。

addCheckboxItems : function(form){
    var newBox = ('<input type="checkbox" name="checkBoxItem" onCheck="JavaScript:addRowItems(this)"; id="checkBoxItemId" value="' + form.combobox.value +'"'+ '>'+form.combobox.value);
    document.getElementById("divCheckbox").innerHTML = newBox;

    }
    addRowItems : function(element){
    alert('Hi');
    if(document.getElementById("checkBoxItemId").checked){
        var tableRef =  document.getElementById('tbl').getElementsByTagName('tbody')[0];
        var newRow   = tableRef.insertRow(tableRef.rows.length);
        var td1 = document.createElement("td")
        var strHtml1 = document.getElementById('checkBoxItemId').value;
        td1.innerHTML = strHtml1.replace(/!count!/g,count);

        var td2 = document.createElement("td")
        var strHtml2 = "NA";
        td2.innerHTML = strHtml2.replace(/!count!/g,count);

        var td3 = document.createElement("td")
        var strHtml3 = "<a>Remove</a>";
        td3.innerHTML = strHtml3.replace(/!count!/g,count);

        newRow.appendChild(td1);
        newRow.appendChild(td2);
        newRow.appendChild(td3);

        tableRef.appendChild(newRow);
    }
}

我怎样才能做到这一点.....提前感谢..

首先,您的语法给出了一个想法,即您在某些对象addCheckboxItems : function(form)创建函数addCheckboxItems : function(form)但在html事件处理程序中, onCheck搜索该函数的全局范围。 因此,您应该在全局范围内定义诸如addCheckboxItems = function(form)类的addCheckboxItems = function(form)

此外,没有oncheck事件。 onclick事件。

这是一个进行了更正的jsfiddle ,您可以在其中了解(或调试)我在说什么。 但是它在addRowItems内部崩溃,因为在您的代码中没有足够的上下文来构建可运行的应用程序...但是您已使处理程序运行,并且现在可以继续

您好,您可以尝试这样做吗

addCheckboxItems : function(form){
var newBox = ('<input type="checkbox" name="checkBoxItem"
onchange="JavaScript:addRowItems(this)"; 
id="checkBoxItemId" value="' + form.combobox.value +'"'+
 '>'+form.combobox.value);
 document.getElementById("divCheckbox").innerHTML = newBox;

}

我添加了input元素的onchange事件。 这应该可以解决问题。

编辑

您也可以尝试使用checkboxonclick事件。 在IE中, change事件的行为似乎有所不同,因此许多用户更喜欢click事件,该事件在input元素的状态发生更改后触发。 您可以参考从onclick / onchange事件获取HTML Checkbox的价值

暂无
暂无

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

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