开发者

jQuery attr command

开发者 https://www.devze.com 2023-01-01 17:38 出处:网络
I want to add a value to the onchange event, but the JS code has errors $(a开发者_如何学编程rgs).attr(\'onChange\', \'setTimeout(\'__doPostBack(\\\'ctl00$cpBody$txtDate\\\',\\\'\\\')\', 0)\');

I want to add a value to the onchange event, but the JS code has errors

$(a开发者_如何学编程rgs).attr('onChange', 'setTimeout('__doPostBack(\'ctl00$cpBody$txtDate\',\'\')', 0)');

anyone see error?


How about the proper jQuery/ best Javascript way:

$(args).bind('change', function () {
    setTimeout(function () {
        __doPostBack('ctl00$cpBody$txtDate','');
    }, 0);
});

Although why the 0ms timeout period?

You can change your code to:

$(args).bind('change', function () {
    __doPostBack('ctl00$cpBody$txtDate','');
});

With only the most minor functional difference (__doPostBack will be executed immediately instead of ASAP).

It's better to provide a function as the first argument of setTimeout, as opposed to a string. setTimeout is closely related to eval, and eval is bad.


Try this :

$(args).bind('Change','setTimeout('
           __doPostBack(\'ctl00$cpBody$txtDate\',\'\')', 0)');


$(args).bind('change', function(e){
    setTimeout(function(){
       __doPostBack('ctl00$cpBody$txtDate','');
    }, 0);
});

should fit a little bit better.

0

精彩评论

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