[英]How to press Enter without actually touching keyboard, using JavaScript, inside an input/textbox box
I know this has been asked many times and I searched a lot. 我知道这个问题已经被问了很多遍了,我进行了很多搜索。 But solutions are either not working in IE or not meeting my requirements. 但是解决方案要么无法在IE中运行,要么无法满足我的要求。
I have an input box where user can input the name. 我有一个输入框,用户可以在其中输入名称。 The value of input box gets saved into an array split by space. 输入框的值将保存到按空格分割的数组中。
Now I am copying array[0] to input box 1, array[1] to input box 2 ans so on. 现在,我将array [0]复制到输入框1,将array [1]复制到输入框2等等。
<input id="txbox" type="text" value="Hello World" />
array[0]=Hello
array[1]=World
<input id="input1" type="text" value="Hello" style="display:none"/>
<input id="input2" type="text" value="World" style="display:none"/>
As soon as the text gets copied, I want to hit/trigger ENTER inside the input area just after the text, without actually touching the keyboard (through JavaScript). 一旦文本被复制,我想在文本之后立即在输入区域内点击/触发ENTER,而无需实际触摸键盘(通过JavaScript)。 How can I achieve that? 我该如何实现?
PS: the solution should work in IE8 and above and should be in plain JavaScript not jQuery. PS:该解决方案应在IE8及更高版本中运行,并且应使用纯JavaScript而不是jQuery。
Need of Simulating Enter: 需要模拟输入:
I am using below code in my project. 我在我的项目中使用以下代码。 which converts input text from English to Hindi only on pressing enter just after the word. 仅在单词后面按Enter时,才将输入文本从英语转换为印地语。 By this I can transliterate word by word. 这样我就可以逐字地音译。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="https://www.google.com/jsapi">
</script>
<script type="text/javascript">
// Load the Google Transliterate API
google.load("elements", "1", {
packages: "transliteration"
});
function onLoad() {
var options = {
sourceLanguage:
google.elements.transliteration.LanguageCode.ENGLISH,
destinationLanguage:
[google.elements.transliteration.LanguageCode.HINDI],
shortcutKey: 'ctrl+g',
transliterationEnabled: true
};
// Create an instance on TransliterationControl with the required
// options.
var control =
new google.elements.transliteration.TransliterationControl(options);
// Enable transliteration in the textbox with id
// 'transliterateTextarea'.
control.makeTransliteratable(['txtEnglish']);
}
google.setOnLoadCallback(onLoad);
</script>
</head>
<body>
Type in Hindi (Press Ctrl+g to toggle between English and Hindi)<br>
<input type="text" id="txtEnglish" size="20"/>
<input type="text id="txtHindi" size="20" readonly/>
</body>
</html>
But, I have requirement like, user should enter the name in English and it should converts to Hindi and should display in other textbox which is readonly. 但是,我有这样的要求,用户应输入英文名称,然后应将其转换为印地语,并应显示在其他只读文本框中。
create a keypress event 创建按键事件
here's my sample code in my app. 这是我的应用程序中的示例代码。 Hope it helps. 希望能帮助到你。
Keycode 13 is the Enter key 键码13是Enter键
$('.search-input').keypress(function(event) {
var search = $('.search-input').val();
var keycode = (event.keyCode ? event.keyCode : event.which);
if (keycode == '13') {
var len = $('.search-input').val().length;
if (len <= 0) {
$('#search-contents').slideDown();
$('.item-list').append('<div class="col-centered"><h3 class="title"> No Item/s Found.</h3></div>');
} else {
$('#search-contents').slideDown();
$('html,body').animate({
scrollTop: $('#search-contents').offset().top
},
'slow');
searchFunction(search);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.