简体   繁体   English

使用jQuery在textarea中按Enter

[英]Pressing Enter in textarea using jQuery

In an acceptance test, pressing Enter key after text input triggers an event. 在验收测试中,在输入文本后触发事件,然后按Enter键。 I tried to fillIn text and hit enter with the keyEvent. 我尝试填写文本,然后使用keyEvent按下Enter键。

PS: Testem ignores the keyEvent. PS:Testem忽略keyEvent。

HTML: HTML:

<div class="issue-container">
<div class="create-container">
<div class="input-field ui-textarea md-animated ember-view">
<label id="ember1369-label" for="ember1369-input" class="active">Add an issue</label>
<textarea data-test-id="txtid" id="ember1369-input" class="md-textarea" style="height: 21.5px; overflow: hidden;"></textarea></div></div></div>

Acceptance test: 验收测试:

test('review-an-element', async function(assert) {
await fillIn(testSelector('txtid'), 'Input is typed here and then hit enter');
await keyEvent(testSelector('txtid'), 'keypress', 13);}); //hitting enter

My jQuery attempt: 我的jQuery尝试:

var e = jQuery.Event("keyup");
e.which = 13;
e.keyCode = 13;    
await $('.md-textarea').trigger(e);

You need to handle the keyup event, not the keypress event. 您需要处理keyup事件,而不是keypress事件。 The keypress event fires only for keys that produce output, like letter keys. 仅针对产生输出的键(例如字母键)触发keypress事件。 Reference: https://developer.mozilla.org/en-US/docs/Web/Events/keypress 参考: https : //developer.mozilla.org/en-US/docs/Web/Events/keypress

For your acceptance test: 为了您的验收测试:

change 更改

await keyEvent(testSelector('txtid'), 'keypress', 13);

to

await keyEvent(testSelector('txtid'), 'keyup', 13);

Edit: The testSelector() helper is deprecated - https://github.com/simplabs/ember-test-selectors/pull/134 编辑: testSelector()助手已经过时- https://github.com/simplabs/ember-test-selectors/pull/134

My solution with jQuery. 我的jQuery解决方案。

var e = $.Event('keydown', { keyCode: 13 });

test('txtarea', async function(assert){
await fillIn($('.md-textarea')[0], 'text is entered here');
await $('.md-textarea').trigger(e);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM