开发者

TextBox onchange when setting value with jQuery

    $(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>

How can I make the change function on the textbox trigger when I click the button?


You can use .change() (the shortcut) or .trigger(), like this:

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

Or this:

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

Either method works to trigger any change event handlers you've added, you can test it here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜