gpt4 book ai didi

javascript - 让文本框识别从按钮复制​​的输入

转载 作者:行者123 更新时间:2023-11-30 05:33:17 24 4
gpt4 key购买 nike

我对此很陌生,我尝试搜索,找不到答案。

我有几个按钮,它们被分配了一个值,当我点击它们时,它会将文本复制到另一个文本框,下面有一个提交按钮。

我无法让文本框识别该值已输入到文本框中,因此无法启用提交按钮。

我所拥有的示例:

$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
});

$('#textBox').on('input', function () {
if ($(this).val().length>0);
$('#submitBtn').removeAttr('disabled');
$('#submitBtn').removeClass('disabled');
});

非常感谢任何帮助。

最佳答案

可以在this StackOverflow question中读到, jQuery 在使用 .val() 方法时不会触发事件。这意味着您的代码

$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
});

不会触发“输入”或“更改”事件。您可以自己触发事件:

$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
$('#textBox').trigger('input');
});

或者,您也可以轻松地从 buttonToCopy 调用函数来禁用按钮:

$("#buttonToCopy").click(function () {
$('#textBox').val("Value").html();
EnableSubmitButton()
});

$('#textBox').on('input', function () {
EnableSubmitButton()
});

function EnableSubmitButton(){
if ($(this).val().length>0) {
$('#submitBtn').removeAttr('disabled');
$('#submitBtn').removeClass('disabled');
}
}

关于javascript - 让文本框识别从按钮复制​​的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25509002/

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