开发者

jQuery javascript binding and calling issue

I have a JavaScript function named 'mc_change'. Also, I have a textbox whose change event should call that JavaScript function

<input type="text" id="mc_val" onchange="javascript:mc_change();" />

It's not working

Can anyone suggest me same开发者_StackOverflow中文版 normal JavaScript or jQuery?

Thanks


As the others have said, your script is valid, and should do what is intended. However, I am guessing that the event you want is probably not the onchange event. In my dealings with text input, I've found that binding to onchange will only do something when focus is lifted from the text input, so using a combination of events to cover several methods of input might be your best bet.

The following is an example of the events I generally use to do something when the text changes:

$('#mc_val').bind({
   change : mc_change,
   keydown : mc_change,  // for key presses
   mouseup : mc_change,  // for mouse paste
   mousemove : mc_change // for mouse paste
});


Your example script is perfectly valid - there are no obvious errors in what you've provided.

Using 'normal' JavaScript you can attach the event with:

document.getElementById('mc_val').onchange = mc_change;

Using jQuery:

$('#mc_val').change(mc_change);

Of course, there is no reason to believe this will fix the problem, since they should all be (more or less) equivalent to your example. "It's not working" doesn't really help.


remove the 'javascript:'... it should be:

<input type="text" id="mc_val" onchange="mc_change();" />


I forgot which browser does this (the IE's, I think), but the change event is deferred until the input loses focus. Try clicking on something else and see if it executes.

I found this mentioned in jQuery's change method documentation.

I got around the issue by using key-press instead (though I wasn't happy about it!).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜