简体   繁体   English

html表单调用javascript函数,并且其提交按钮返回false

[英]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.

相关问题 单击HTML表单提交按钮将调用一个函数。 提交表格之前,功能是否总是会完成? - Clicking HTML form submit button calls a function. Will function always complete before form is submitted? 如何使用 JavaScript 函数向 HTML 表单添加提交按钮? - How to add submit button to an HTML form with JavaScript function? 当我的javascript函数返回false时,为什么我的表单仍然提交? - Why does my form still submit when my javascript function returns false? 通过 Javascript 代码单击 HTML 表单的提交按钮 - Clicking submit button of an HTML form by a Javascript code 表单提交按钮onClick JavaScript功能 - Form submit button onClick JavaScript function 用 1 个按钮提交一个 php 表单和一个 javascript 函数 - submit a php form and a javascript function with 1 button Javascript调用表单提交 - &gt;调用 - &gt; Javascript(ajax调用)没有从ajax调用返回 - Javascript calls form submit -> calls -> Javascript (ajax call) not getting returns from ajax call 在html提交按钮中调用javascript函数 - call javascript a function in html submit button 使用javascript提交HTML表单vs提交按钮 - html form submit using javascript vs submit button 在Spring MVC中单击表单提交和调用提交按钮的JavaScript函数? - Form submit and javascript function calling on submit button click in spring mvc?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM