在提交该页面之前检查表单字段值 [英] Checking the form field values before submitting that page

查看:37
本文介绍了在提交该页面之前检查表单字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

//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>

提前致谢

推荐答案

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

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

您可能需要考虑在表单标记中使用 onsubmit="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.

这篇关于在提交该页面之前检查表单字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆