[英]html form calls javascript function and its submit button returns false
I have a html form which calls a java script function, this is the form tag: 我有一个HTML表单,它调用Java脚本函数,这是表单标签:
<form name="myForm1" method="post" action="test" onsubmit=" updateTable();">
and the submit button of this form inserts data in the database, but after inserting it moves to a blank page, I want it to stay in the same page to insert more data, this is the javascript function: 并且此表单的“提交”按钮将数据插入数据库中,但是插入后移动到空白页,我希望它留在同一页中以插入更多数据,这是javascript函数:
function updateTable() {
tabBody = document.getElementById("editable");
row = document.createElement("tr");
cellID = document.createElement("td");
cellname = document.createElement("td");
cellID.innerHTML = document.forms['myForm1'].elements[1].value;
cellname.innerHTML = document.forms['myForm1'].elements[0].value;
row.appendChild(cellID);
row.appendChild(cellname);
if (tabBody.childNodes.length == 10) {
tabBody.removeChild(tabBody.childNodes[0])
}
document.getElementById("mytb").style.display = "block";
tabBody.appendChild(row);
}
and this is the code of the button: 这是按钮的代码:
<input id="SaveBtn" name="SaveBtn" type="submit" value="حفظ" onclick="return false;">
and return false doesn't work. 并返回false不起作用。
Does any one have a solution? 有没有人有办法解决吗?
Have you tried preventDefault()
? 您是否尝试过preventDefault()
?
function updateTable(e) {
e.preventDefault();
tabBody = document.getElementById("editable");
row = document.createElement("tr");
cellID = document.createElement("td");
cellname = document.createElement("td");
cellID.innerHTML = document.forms['myForm1'].elements[1].value;
cellname.innerHTML = document.forms['myForm1'].elements[0].value;
row.appendChild(cellID);
row.appendChild(cellname);
if (tabBody.childNodes.length == 10) {
tabBody.removeChild(tabBody.childNodes[0])
}
document.getElementById("mytb").style.display = "block";
tabBody.appendChild(row);
return false;
}
EDIT: As pointed out in comment below. 编辑:正如在下面的评论中指出。 You'll have to pass the event too. 您也必须通过活动。
<form name="myForm1" method="post" action="test" onsubmit=" updateTable(event);">
You should add return false;
您应该添加return false;
at the end of your updateTable()
function. 在updateTable()
函数的末尾。
This will prevent the form submission to complete. 这将阻止表单提交完成。
If return false doesn't work. 如果返回false无效。 You can also try to get the first argument (the event) and prevent the default action on it: 您还可以尝试获取第一个参数(事件)并阻止对其执行默认操作:
function updateTable( event ){
// do your stuff
// ...
event.preventDefault();
return false;
}
Don't use the onsubmit
property. 不要使用onsubmit
属性。
<form name="myForm1" method="post" action="test" onsubmit=" updateTable();">
Use onclick
like below. 使用如下所示的onclick
。 Control will remain in same page. 控件将保留在同一页面中。
<form name="myForm1" onclick=" updateTable();">
In the updateTable()
function use AJAX call to insert the data. 在updateTable()
函数中,使用AJAX调用来插入数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.