开发者

Updating attr("onclick") issue

I have the next code to work on ASP.Net controls.

I'm trying to add some javascript code to "onclick" event on a button when the result variable(res) is equals to "NO". The issue is that the first time it assigns the value to "onclick" it works fine, but when the removeAttr is executed and again the function is call and the res is equals to "NO" again it doesn't add the code on "onclick" event. So what's wrong on this? Thanks for your help. =)

Legend: - HLevel(Textbox) - PSave(LinkButton)

 $('#HLevel').blur(f开发者_Python百科unction () {
      // SOME CODE HERE BEFORE THE ISSUE CODE
      var res = $(msg).find("string").text();
      if (res == "NO") {
         $('#PSave').attr("onclick", "javascript:return confirm('some text');");
      } else {
        $('#PSave').removeAttr("onclick");
      }

  });


Use the unbind() and click() methods:

if (res == "NO") {
    $('#PSave').click(function() {return confirm('some text');});
} else {
    $('#PSave').unbind("click");
}

If you use jQuery, try to make use of it as much as possible and don't mix styles.


You need to change the click event handler like so:

$('#HLevel').blur(function () {
     // SOME CODE HERE BEFORE THE ISSUE CODE
     var res = $(msg).find("string").text();
     if (res == "NO") {
        $('#PSave').click(function(){
            return confirm('some text');
        });
     } else {
        $('#PSave').unbind('click');
     }

 });


why not use jquery for it all instead of javascript onclick handlers. those are messy.

$('#PSave').click(function(){
    confirm('some text');

});

and instead of removeAttr use unbind.

$('#PSave').unbind();
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜