当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - How to Input type text as submit button in html?

问题描述:

I have a form without input type submit .

<form class="form" action="" method="post" name="new-service"

enctype=multipart/form-data>

<span class="pull-right">

<input id="sa" name="Searchs" type="text" placeholder="Analyze Search"

class="form-control input-xlarge search-query center-display"

required="">

<span class="pull-right"> <a class="btn btn-success" href="#"

onclick="

this.href='/recrawl?search=' + document.getElementById('sa').value;

somefunction(this, event);

return true;

"> Analyze Data </a>

<br><br>

</span>

</form>

above form work when click on Analyze Data .

but now I want It run on press Enter of Input type text tag

I tried

<input id="sa" name="Searchs" type="text" placeholder="Analyze Search"

class="form-control input-xlarge search-query center-display" required=""

onclick="

this.href = '/recrawl?search=' + document.getElementById('sa').value;

somefunction(this, event);

return true;

">

but it is not working ?

网友答案:
<input id="sa" name="Searchs" type="text" placeholder="Analyze Search" class="form-control input-xlarge search-query center-display" required="" onKeyPress="postURL(event, this)">

function postURL(e, textarea){
   var code = (e.keyCode ? e.keyCode : e.which);
   if(code == 13) { //Enter keycode
      window.location.href ='/recrawlsearch='+document.getElementById('sa').value;
   }
}

Hope this window.location.href would help you.

网友答案:

The input element don't have the href attribute, so, this.href = xxxx is not working.

If you want to click input element, use window.location.href = xxx.

分享给朋友:
您可能感兴趣的文章:
随机阅读: