Javascript返回false不起作用

Lea*_*ner 2 javascript

这个代码在提交后在空白页面中返回false.我的代码出错了.

function send_serial(){
    //Check date
    var date=document.getElementById('date_field').value;
    if(date==''||date==null){
    alert('Please select the date.');
        return false;
  }  

}
Run Code Online (Sandbox Code Playgroud)

Mik*_*uel 8

你的表格标签应该是

<form onsubmit="return send_serial();"> 
Run Code Online (Sandbox Code Playgroud)

<form action="JAVASCRIPT:send_serial();"> 
Run Code Online (Sandbox Code Playgroud)

当您JAVASCRIPT:send_serial用作操作时,您要求表单重新提交到其内容由send_serial函数结果提供的页面.

onsubmit事件处理程序的JavaScript,而不是一个URL,所以不需要JAVASCRIPT:在前面.如果事件处理程序的结果为false,则它将取消表单提交.但你不能只说,onsubmit="send_serial()"因为那时动作处理程序的结果就没有了.动作处理程序基本上是一个插入的函数体,function (event) { ... }因此你需要returnonsubmit属性中有一个.