开发者

jquery bind event

开发者 https://www.devze.com 2022-12-09 02:35 出处:网络
html: <ul> <li><input type=\"submit\" id=\"myId\" value=\"someVal\"/> </ul> jQuery

html:

<ul>
    <li><input type="submit" id="myId" value="someVal"/>
</ul>

jQuery

$(开发者_如何学Go'ul').find('input[type="submit"]').click(function{
         alert('nasty alert')
         $(this).attr('id','newId');
});

$('input#newId').click(function(){
          $(this).hide();
});

ok, so my intention is to change the id after one click and then the button to do something else(hide). i tried with live() too. In firebug it looks like the id has changed but my second click on the button triggers the same alert('nasty alert'). And something strange...if i use live(), on mouse right click the button dissapears (like it should). any sugestions? thanks


You're basically attaching a click event handler twice to the same input.

There's no reason you should attach two event handlers, I've updated the code so a variable is used to keep track.

Edit: Fixed syntax error and now it's using .data

<ul>
<form>
<input type=submit value=go>
</form>
</ul>
<script src=http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js></script>
<script>


    $('ul').find('input[type="submit"]').click(function() {
             if ( !$(this).data('_clicked') ) {
                 alert('nasty alert')
                 $(this).attr('id','newId');
                 $(this).data('_clicked', true);
             } else {
                 $(this).hide();
             }
             return false;
    });
</script>
0

精彩评论

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