如何在按键上提交表单? [英] How to submit form on keypress?

查看:115
本文介绍了如何在按键上提交表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用此脚本在静态页面中搜索



但是我想要这个搜索当我输入文本输入,而不是当我点击按钮时,我searchd,我发现任何这将工作:

  onkeypress =this.submit(); 
onkeyup =this.submit();
onkeypress =document.forms [f1]。submit();
onkeyup =document.forms [f1]。submit();

但没有一个可行



我使用与脚本相同的html

 < form id =f1name =f1action =javascript:void ()onsubmit =if(this.t1.value!= null& amp; amp; this.t1.value!='')parent.findString(this.t1.value); return false;> ; 
< input type =textid =t1name =t1value =Searchonfocus =if(this.value =='Search')this.value =''; size =20onkeypress =this.submit(); />
< input type =submitname =b1value =Find/>
< / form>


解决方案

form.submit的onsubmit 即可。你应该实现一个函数。



你的onkeyup脚本是反直觉的,因为选择onkeyup文本需要模糊文本框焦点。



使用您的片段创建测试,该片段调用 findString(this.value); ,而不是提交:

http://jsfiddle.net/e9Esz/

 一些示例文本
< input type =textid =t1name =t1value =Searchonfocus =if(this.value =='Search')this.value =''; size =20onkeyup =findString(this.value); />
< input type =submitid =b1name =b1value =Find/>
< / form>

Javascript:

  var TRange = null; 

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

//为浏览器支持的代码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 browser does not supported,sorry ...)
return ;

if(!strFound)alert(String'+ str +'not found!)
return;
}


I use this script to search in a static page

But i want this to search when i type in the text input and not when i click the button, i searchd and i found that any of this would work:

onkeypress="this.submit();"
onkeyup="this.submit();"
onkeypress="document.forms["f1"].submit();"
onkeyup="document.forms["f1"].submit();"

but none of them works

i used the same html with the script's

<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="Search" onfocus="if(this.value=='Search')this.value='';" size="20" onkeypress="this.submit();" />
<input type="submit" name="b1" value="Find" />
</form>

解决方案

form.submit() does not trigger onsubmit. You should implement a function instead.

Your onkeyup script is counter-intuitive though, since selecting the text onkeyup requires blurring of the textbox focus.

Created a test using your snippets that calls findString(this.value); instead of submit:

http://jsfiddle.net/e9Esz/

some sample text
<form id="f1" name="f1" action="javascript:void(0)" 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="Search" onfocus="if(this.value=='Search')this.value='';" size="20" onkeyup="findString(this.value);" />
<input type="submit" id="b1" name="b1" value="Find" />
</form>

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 ("String '"+str+"' not found!")
 return;
}

这篇关于如何在按键上提交表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆