Javascript 在提交该页面之前检查表单字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3279628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:00:43  来源:igfitidea点击:

Checking the form field values before submitting that page

javascripthtmlajaxjsp

提问by reva

I have written following function which checks whether start_date field is not empty and displays proper message when submit button is clicked. But then it takes the control to the previous page. So user has to write again all other fields on that form. Is there any way to stay on that page even after prompting the error message, with all other fields value.

我编写了以下函数,用于检查 start_date 字段是否为空,并在单击提交按钮时显示正确的消息。但随后它将控制权带到上一页。因此用户必须再次在该表单上写入所有其他字段。即使在提示错误消息后,有没有办法留在该页面上,所有其他字段的值。

//JavaScript
function checkform() {
    if(document.frmMr.start_date.value == "") {
        alert("please enter start_date");
        return false;
    } else {
        document.frmMr.submit();
    }
}

// HTML
<html>
    <form name=frmMr action="page1.jsp">
        Enter Start date:
        <input type="text" size="15" name="start_date" id="start_date">
        <input type="submit" name="continue" value="submit" onClick="checkform();">
    </form>
</html>

Thanks in advance

提前致谢

回答by user11977

While you have a return value in checkform, it isn't being used anywhere - try using onclick="return checkform()"instead.

虽然您在 checkform 中有一个返回值,但它并没有在任何地方使用 - 尝试使用onclick="return checkform()"

You may want to considering replacing this method with onsubmit="return checkform()"in the form tag instead, though both will work for clicking the button.

您可能需要考虑onsubmit="return checkform()"在表单标记中替换此方法,尽管两者都适用于单击按钮。

回答by Ujwal Ratra

You can simply make the start_date required using

您可以简单地使用 start_date

<input type="submit" value="Submit" required />

You don't even need the checkform() then.

那么你甚至不需要 checkform() 。

Thanks

谢谢

回答by nikhil reddy

use return before calling the function, while you click the submit button, two events(form posting as you used submit button and function call for onclick) will happen, to prevent form posting you have to return false, you have did it, also you have to specify the return i.e, to expect a value from the function,

在调用函数之前使用 return,当你点击提交按钮时,会发生两个事件(表单发布,因为你使用提交按钮和函数调用 onclick),为了防止表单发布,你必须返回 false,你已经做到了,你也是必须指定返回值,即期望函数的值,

this is a code:

这是一个代码:

input type="submit" name="continue" value="submit" onClick="**return** checkform();"

回答by WSkid

Don't know for sure, but it sounds like it is still submitting. I quick solution would be to change your (guessing at your code here):

不确定,但听起来它仍在提交。我的快速解决方案是更改您的(在这里猜测您的代码):

<input type="submit" value="Submit" onclick="checkform()">

to a button:

到一个按钮:

<input type="button" value="Submit" onclick="checkform()">

That way your form still gets submitted (from the else part of your checkform()) and it shouldn't be reloading the page.

这样你的表单仍然会被提交(从你的 checkform() 的 else 部分)并且它不应该重新加载页面。

There are other, perhaps better, ways of handling it but this works in the mean time.

还有其他可能更好的方法来处理它,但这同时也有效。