表单多次提交

时间:2013-11-12 03:44:36

标签: javascript html jsfiddle

在表单内部单击时,表单自动尝试重新提交,我无法弄清楚代码的哪一部分会导致它像这样。在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);
 }

3 个答案:

答案 0 :(得分:1)

可能是<form onClick="...">

它的用途是什么?

请考虑onsubmit

答案 1 :(得分:0)

你写过了。代表点击事件的表单。

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

你应该做的是

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

Updated Fiddle

答案 2 :(得分:0)

每次单击表单时,都会调用两个函数。您的表位于表单内部,因此单击表中的任何元素也将调用函数。你应该只在提交表格时打电话给那些。

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

应该是

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