javascript 错误消息未显示

标签 javascript

如果文本框 (First_Name) 留空,我会尝试出错。例如,如果名字字段留空,用户将收到以下错误:“请输入您的名字”,但在将该字段留空后单击提交按钮时没有任何反应...

<HTML>
<HEAD>
<TITLE>this is a test page</TITLE>
<script language="javascript" type="text/javascript">
<!--
function CheckForm()
{
  var formObj = document.getElementById("Data");
  var firstname = formObj.FIRST_Name.value;

    if(notEmpty(firstname, "Please enter your first name")){                        

    return true;}

    return false;
    }

function notEmpty(elem, helperMsg){
    if(elem.value.length == 0){
        alert(helperMsg);
        elem.focus(); // set the focus to this input
        return false;
    }
    return true;
}

</script>
</HEAD>
<BODY>
<div style="background: #CCCC99">
<HR><FORM id="Data" onsubmit="return CheckForm()" >
<P>First Name: <input type=text name=FIRST_Name maxlength=15 size=15>
</P>
<input type=submit value="Submit Products Registration Form" style="width: 220px"><input type=reset value="Reset">
</form>
</div>
</BODY>
</HTML>

最佳答案

firstname 是元素值,但您将它作为元素传递给 notEmpty,而不是使用

if(notEmpty(formObj.FIRST_Name, "Please enter your first name")){ 

关于javascript 错误消息未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11660934/

相关文章:

javascript - 使用 Node js 抓取 <ul> 中的所有列表 <li> 项

javascript - jQuery:获取最后两个列表项?

javascript - D3.js v7 - 如何使可滚动图表的 Y 轴标签始终显示在屏幕上

javascript - 从 setTimeout 中调用 React 组件方法

javascript - jquery 委托(delegate)属性更改

javascript - 仅使用 CSS 关闭模式

javascript - 不一致错误: collection was modified enumeration operation may not execute

javascript - 以全高清渲染 div 内容,然后按比例缩放

javascript - 如何使用 javascript 停止触发事件

javascript - 为多级树实现 d3-2way-tree