gpt4 book ai didi

javascript - 当通过鼠标操作更改浏览器文本输入时使用react

转载 作者:行者123 更新时间:2023-11-28 02:23:11 25 4
gpt4 key购买 nike

我有一个文本输入字段和一个复选框。当且仅当文本字段中有输入时,必须禁用该复选框。我有一个适用于大多数场景的解决方案:

HTML:

<input type="text" id="search" />
<input type="checkbox" id="cb" />
<label for="cb">Enabled only if no search term</label>

jQuery:

$('#search').keyup(function (e) {
var enable = $(this).val() == 0;
if (enable) {
$('#cb').removeAttr('disabled');
} else {
$('#cb').attr('disabled', 'disabled');
}
});

查看live on jsFiddle .

当使用键盘输入或删除文本以及使用键盘粘贴文本时,此功能有效。

如果用户使用右键单击上下文菜单粘贴文本,或者用户按下 IE 添加到输入字段以允许清除输入字段的小“X”,则会失败。

问题

如何改进代码,使其在这些场景中也能正常工作?等待文本框失去焦点会提供较差的用户体验。

最佳答案

Real Time Validation jQuery 插件解决了这个问题。然而,它确实not seem to work currently与委托(delegate)事件。

Implementing this requires binding events to the keyup event, and a couple other events if you want to detect text changes on cut and paste. Even if you're a JavaScript god it's tedious to keeping writing this logic over and over again. Be smart and use the ZURB text change event plugin instead.

关于javascript - 当通过鼠标操作更改浏览器文本输入时使用react,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15347692/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com