开发者

Javascript return false not working

开发者 https://www.devze.com 2023-03-16 16:59 出处:网络
Guys this code is returning false in a blank page after submit.What is the error of my code. function send_serial(){

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

精彩评论

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