简体   繁体   中英

JavaScript return false onsubmit not working

var TRange=null;

function findString (str) {
  if (parseInt(navigator.appVersion)<4) return;
  var strFound;
  if (window.find) {

    / CODE FOR BROWSERS THAT SUPPORT window.find

    strFound=self.find(str);
    if (!strFound) {
      strFound=self.find(str,0,1);
      while (self.find(str,0,1)) continue;
    }
  }
  else if (navigator.appName.indexOf("Microsoft")!=-1) {

    // EXPLORER-SPECIFIC CODE

    if (TRange!=null) {
      TRange.collapse(false);
      strFound=TRange.findText(str);
      if (strFound) TRange.select();
    }
    if (TRange==null || strFound==0) {
      TRange=self.document.body.createTextRange();
      strFound=TRange.findText(str);
      if (strFound) TRange.select();
    }
  }
  else if (navigator.appName=="Opera") {
    alert ("Opera browsers not supported, sorry...")
    return;
  }
  if (!strFound) alert ("Word '"+str+"' Not Found")
  return;
}

page refreshes as soon as I tap in searchbox in mobile browser but works with PC after entering a string and hitting enter

<form id="f1" name="f1" action="javascript:void()"
    onsubmit=" if(this.t1.value!=null &amp;&amp; this.t1.value!='')
                 parent.findString(this.t1.value);return false;">

  <input type="text" id="t1" name="t1" value="" placeholder="Find Words" size="20">
  <input type="submit" name="b1" value="Find">
</form>

event.preventDefault usage:

<form name="f1" action="" >

  <input type="text"  name="t1" value="" placeholder="Find Words" size="20">
  <input type="submit" value="Find">

</form>
document.forms.f1.addEventListener('submit',function(event)
  {
  event.preventDefault() // disable submit

  
  console.log( this.t1.value )
  
    // ...
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM