使用javascript(jQuery)设置值时TextBox onchange

时间:2010-07-08 10:36:57

标签: jquery javascript-events textbox input onchange

    $(document).ready(function() {
        $('#text').live('change', function() {
            alert('hello');
        });
        $('#button').live('click', function() {
            $('#text').val('some text');
        });
    });  

    <div>
        <input type="button" id="button" value="Click Me" />
        <input type="text" id="text" />
    </div>

单击按钮时,如何在文本框触发器上创建change功能?感谢

1 个答案:

答案 0 :(得分:5)

您可以使用.change()(快捷方式)或.trigger(),如下所示:

$('#button').live('click', function() {
  $('#text').val('some text').change();
});

或者这个:

$('#button').live('click', function() {
  $('#text').val('some text').trigger('change');
});

任何一种方法都可以触发您添加的任何change event handlers you can test it here

相关问题