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

我有几个为按钮分配了值的按钮,当我单击它们时,它将文本复制到另一个文本框中,并在其下方提供了一个提交按钮。

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

我所拥有的样本:

$("#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中读取,使用.val()方法时,jQuery不会触发事件。这意味着您的代码

$("#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');
   }
}

07-24 09:44
查看更多