它没有在我的提交按钮上使用javascript进行验证

它没有在我的提交按钮上使用javascript进行验证,javascript,jquery,Javascript,Jquery,我有这个代码在我看来 function Validate() { if (document.getElementById('MandateName').value == "") { var err = document.getElementById('MandateNameErr'); err.innerHTML = "Please enter a value for the Mandate Name"; err.style.display =

我有这个代码在我看来

function Validate() {
   if (document.getElementById('MandateName').value == "") {

       var err = document.getElementById('MandateNameErr');
       err.innerHTML = "Please enter a value for the Mandate Name";
       err.style.display = "block";
       return false;
   }
   else {
       document.getElementById('MandateNameErr').style.display = "none";
   }

   if (document.getElementById('MandateDescription').value == "") {
       var err = document.getElementById('MandateDescriptionErr');
       err.innerHTML = "Please enter a value for the Mandate Description";
       err.style.display = "block";
       return false;
   }
   else {
       document.getElementById('MandateDescriptionErr').style.display = "none";
   }

   return true;
}
我是否在提交前验证了“提交”按钮

<button name="Submit" onclick="Validate()" >Add Variables to Mandate</button>
将变量添加到任务中
我调用了Validate函数,但如果我没有在文本框中输入任何内容,它会向我显示,如果我单击按钮,它会向我显示验证消息,但同时它会转到我的视图并向我抛出消息


甚至我也把报税表弄错了;它不工作是因为我做错了什么?

您需要在
onclick
中输入
return
,如下所示:

<button name="Submit" onclick="return Validate()" >Add Variables to Mandate</button>
将变量添加到任务中

否则,您将执行验证…但并不真正关心结果。

@Nick……忘记返回,调用js函数时最常见的错误是最常见的错误吗?不推荐的附加事件样式中的一个非常常见的错误如何?如果您将脚本与html分开,则不会发生这种情况。@Juan-虽然我同意,但我发现您的参数(
$(“按钮”)有问题。单击(function(){Validate();})
将有完全相同的问题,它需要是
$(“按钮”)。单击(函数(){return Validate();})
$(“按钮”)。单击(验证)我明白了。。。我猜返回false以阻止默认操作是不应该使用的,而应该使用preventDefaultName为什么要删除代码?现在这个问题没有那么有意义了。。。