-1
嗨我想模擬用戶單擊輸入框。 腳本是:在Javascript中使用jquery模擬按鍵
function prepareToScan(){
$("fireRegAdd").focus();
$('fireRegAdd').keypress();
}
和HTML是
<h2>Add a visitor to today's fire register</h2>
<input type="button" onmousedown="prepareToScan()" value="Add Visitor">
<input id="fireRegAdd" name="fireRegAdd">
</div>
我有一個條形碼掃描儀這使代碼到輸入框中,但只有當它已經「點擊」。我試過.focus(),但它需要兩次掃描才能使掃描工作。聚焦點與實際點擊框不同。有誰知道這是怎麼做到的嗎?
感謝
所以我發現,如果我在警報添加它正確設置:
function prepareToScan(){
alert("ready to scan");
$("fireRegAdd").focus();
$('fireRegAdd').keypress();
}
,但我真的不希望一個警告框
我已經添加了代碼演示。當你點擊按鈕時,我希望在輸入框中有一個閃爍的光標。
function prepareToScan(){
\t $("#fireRegAdd").focus();
\t $('#fireRegAdd').click();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div>
<h2>Add a visitor to today's fire register</h2>
<input type="button" value ="Add Visitor" onmousedown="prepareToScan()"/>
<input id="fireRegAdd" name="fireRegAdd" value="" />
</div>
可能重複:http://stackoverflow.com/questions/2381572/how-can-i-trigger-a-javascript-event-click – ToastyMallows 2015-03-30 19:57:01
你試過'。點擊()'?另外,不要忘記你的'#'在jQuery選擇器中的id前面。 – 2015-03-30 19:58:01
我正在使用我不認爲支持的mootools。點擊 – user1616338 2015-03-30 20:18:03