简体   繁体   English

是否可以使用复选框或单选按钮代替提交按钮?

[英]Is it possible to use a check box or radio button in place of a submit button?

I have a form that allows me to dynamically add additional rows to a form using javascript but the button associated with adding new rows is too "bulky" and the script already incorporates the use of a check box that has to be checked in order to delete any rows associated with it, So I want to know if it's possible to use the check box to add and remove rows instead of a button? 我有一个表单,该表单允许我使用javascript动态向表单中添加其他行,但是与添加新行相关的按钮过于“庞大”,该脚本已经包含了一个复选框,该复选框必须选中才能删除与之关联的任何行,所以我想知道是否可以使用复选框来添加和删除行而不是按钮? Here is the jsFiddle of the script http://jsfiddle.net/2gPfn/9/ and the code: 这是脚本http://jsfiddle.net/2gPfn/9/的jsFiddle和代码:

 function addRow(tableID) {
        var table = document.getElementById(tableID); 
        var rowCount = table.rows.length;
        var row = table.insertRow(rowCount);
        var colCount = table.rows[0].cells.length;
        for(var i=0; i<colCount; i++) {
            var newcell = row.insertCell(i);             
            newcell.innerHTML = table.rows[0].cells[i].innerHTML;
            newcell.children[0].name = newcell.children[0].name + rowCount;
            console.log(newcell.children[0].name);
            switch(newcell.childNodes[0].type) {
                case "text":
                        newcell.childNodes[0].value = "";
                        break;
                case "checkbox":
                        newcell.childNodes[0].checked = false;
                        break;
                case "select-one":
                        newcell.childNodes[0].selectedIndex = 0;
                        break;
            }
        }
    }

    function deleteRow(tableID) {
        try {
        var table = document.getElementById(tableID);
        var rowCount = table.rows.length;

        for(var i=0; i<rowCount; i++) {
            var row = table.rows[i];
            var chkbox = row.cells[0].childNodes[0];
            if(null !== chkbox && true === chkbox.checked) {
                if(rowCount <= 1) {
                    alert("Cannot delete all the rows.");
                    break;
                }
                table.deleteRow(i);
                rowCount--;
                i--;
            }


        }
        }catch(e) {
            alert(e);
        }
    }

Basically what I want to do is to add a new row the user will check the box and a new row will be added with a new check box. 基本上我想做的是添加新行,用户将选中该框,新行将添加一个新复选框。 If they uncheck the previous checkbox the row will be deleted. 如果他们取消选中上一个复选框,则该行将被删除。 Is this possible? 这可能吗?

Not sure I really understand why you'd want to do this. 不知道我是否真的了解您为什么要这样做。 But, if I'm understanding you correctly: 但是,如果我正确地理解了您:

http://jsfiddle.net/2gPfn/13/ http://jsfiddle.net/2gPfn/13/

<INPUT type="checkbox" name="chk" onclick="doIt(this, 'dataTable')">
function doIt(el, tableID) {
    if (el.checked) {
        addRow(tableID);
    } else {
        deleteRow(el);
    }
}

function addRow(tableID) {
    /*  same as original */
}

function deleteRow(el) {
    var row = el.parentNode.parentNode;
    row.parentNode.removeChild(row);
}

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

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