1

在表单内部单击时,表单会自动尝试重新提交,我无法弄清楚代码的哪一部分导致它出现这样的行为。在 createTable 函数中,在给定域之后创建一个表。但我无法选择输出中的任何控件。我在这里附上了 jsfiddle 代码链接:http: //jsfiddle.net/rasikaceg/S7kWM/

function createTable() {
document.getElementById("table_container").innerHTML = "";
var input_domain = document.forms["form1"]["DomainName"].value;
if (input_domain == null || input_domain == "") return;

var table = document.createElement("table"),
    tablehead = document.createElement("thead"),
    theadrow = document.createElement("tr"),
    th1 = document.createElement("th"),
    th2 = document.createElement("th"),
    th3 = document.createElement("th"),
    th4 = document.createElement("th");
th1.appendChild(document.createTextNode("Website"));
th2.appendChild(document.createTextNode("Enable/Disable Live Update for LM and CBD"));
th3.appendChild(document.createTextNode("From Date"));
th4.appendChild(document.createTextNode("To Date"));
theadrow.appendChild(th1);
theadrow.appendChild(th2);
theadrow.appendChild(th3);
theadrow.appendChild(th4);
tablehead.appendChild(theadrow);
table.appendChild(tablehead);

var names = ["website1", "website2"];
var container = document.getElementById("table_container");
var tablebody = document.createElement("tbody");
for (var i = 0, len = names.length; i < len; ++i) {
    var row = document.createElement("tr"),
        column1 = document.createElement("td"),
        column2 = document.createElement("td"),
        column3 = document.createElement("td"),
        column4 = document.createElement("td"),
        checkbox = document.createElement('input');
    checkbox.type = "checkbox";
    checkbox.name = names[i];
    checkbox.value = names[i];
    checkbox.id = names[i];
    var label = document.createElement('label')
    label.htmlFor = names[i];
    label.appendChild(document.createTextNode(names[i]));
    column1.appendChild(checkbox);
    column1.appendChild(label);

    var dropdown = document.createElement("select");
    dropdown.name = names[i] + "_select";
    var op1 = new Option();
    op1.value = "enable";
    op1.text = "enable";
    var op2 = new Option();
    op2.value = "disable";
    op2.text = "disable";
    dropdown.options.add(op1);
    dropdown.options.add(op2);
    column2.appendChild(dropdown);

    var datetime_from = document.createElement('input');
    datetime_from.type = "datetime-local";
    datetime_from.name = names[i] + "_from";
    column3.appendChild(datetime_from);


    var datetime_to = document.createElement('input');
    datetime_to.type = "datetime-local";
    datetime_to.name = names[i] + "_to";
    column4.appendChild(datetime_to);


    row.appendChild(column1);
    row.appendChild(column2);
    row.appendChild(column3);
    row.appendChild(column4);
    tablebody.appendChild(row);
}
table.appendChild(tablebody);
document.getElementById("table_container").appendChild(table);
 }
4

3 回答 3

1

可能<form onClick="...">吗?

它是干什么用的?

onsubmit改为考虑。

于 2013-11-12T03:50:37.320 回答
0

你写。它代表点击事件的表单。

<form name="form1" method="post" onClick="createTable();return myFunction();">

你应该做的是

<form name="form1" method="post" action="createTable();return myFunction();">

更新小提琴

于 2013-11-12T03:54:49.943 回答
0

每次单击表单时,都会调用两个函数。您的表格在表格内,因此单击表格中的任何元素也将调用这些函数。您应该仅在提交表单时调用它们。

<form name="form1" method="post" onClick="createTable();return myFunction();">

应该

<form name="form1" method="post" onsubmit="createTable();return myFunction();">
于 2013-11-12T04:00:44.937 回答