JavaScript返回false onsubmit不起作用

时间:2020-10-06 15:54:22

标签: javascript

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;
}

在移动浏览器中点击搜索框后,页面会立即刷新,但是在输入字符串并按Enter键后,PC才能使用

<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>

1 个答案:

答案 0 :(得分:0)

event.prevent默认用法:

<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 )
  
    // ...
  })
相关问题