enter to trigger button click?
I have an html form like this:
<form name="f1">
<center>
<strong>Search VMs:</strong>
<input id='vms' name="word" type="text" size="50" placeholder="Name or IP, space delimited, partial name OK." autofocus autocomplete="off" pattern=".{3,}" required title="3 chars minimum" /> in
<select name="vc" id='whichvc'>
<option value="all" selected="selected">all VCs</option>
</select>
<input type="checkbox" id="check4detail" name="detail">Include details
<input name='searchbutton' value="Search VMs" type="button" onclick='xmlhttpPost("/cgi-bin/search-vm.pl")' />
<input value="Clear" type="button" onclick="clearBox('result')" />
</center>
</p>
<div id="loading"></div>
<div id="result"></div>
</form>
As you can see, there are a text input field and a few other buttons, one of which has a onclick() listener associated to it. How do I trigger the onclick event on that button when user enters text and press enter?
Tried this solution from the forum but it somehow does not work:
$('#vms').keypress(function(event){
if (event.which === 13){
$('#searchbutton').click();}
});
BTW, when I enter a text in the field and click on the search button, it work just fine. But enter text and press enter does not trigger a search event somehow.
修改@GolezTrol代码,以便输入文本字段上的onChange侦听器间接触发单击搜索按钮。
<form action="" method="get">
Test field:
<input type="text" name="test" onChange="aliasonclick">
<br>
<button name="searchbutton" id="clickthis" onclick="alert('it works!');">Submit</button>
<button type="reset" value="Reset">Reset</button>
</form>
<script>
function aliasonclick() {
var searchbutton = document.getElementById("clickthis")
searchbutton.click()
}
</script>
Change $('#searchbutton').click();
to $('#searchbutton').trigger('click');
Change the type of the button to submit
. Submit buttons grab the enter key when it is pressed in one of the edits in the same form. In the example below you can test it. Put the cursor in the edit box and press enter. You should get an alert.
<form action="" method="get" onsubmit="return false;">
Test field: <input type="text" name="test"><br>
<button type="submit" value="Submit" onclick="alert('it works!');">Submit</button>
<button type="reset" value="Reset">Reset</button>
</form>
链接地址: http://www.djcxy.com/p/51630.html
上一篇: Scala的XML平等问题
下一篇: 进入触发按钮点击?