开发者

Javascript return false not working

Guys this code is returning false in a blank page after submit.What is the error of my code.

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

}


Your form tag should be

<form onsubmit="return send_serial();"> 

not

<form action="JAVASCRIPT:send_serial();"> 

When you use JAVASCRIPT:send_serial as the action, you're asking the form to resubmit to a page whose content is supplied by the result of the send_serial function.

The onsubmit event handler is JavaScript, not a URL, so doesn't need the JAVASCRIPT: in front. If the result of the event handler is false, it will cancel form submission. But you can't just say onsubmit="send_serial()" because then the result of the action handler would be nothing. The action handler is basically a function body that is plugged into function (event) { ... } so you need to have a return in the onsubmit attribute.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜