简体   繁体   中英

How can I use a data attribute and jQuery to make a link clickable or not?

I have two buttons on my page:

     <a class="button accessLink"
        id="loginLink"
        data-disabled="false"
        data-href="/MyAccount/Access/Login"
        title="Login">Login</a>

     <a class="button accessLink"
        id="registerLink"
        data-disabled="false"
        data-href="/MyAccount/Access/Register"
        title="Register">Register</a>

How can I use jQuery to make it so that if one of the buttons is clicked and if data-disabled is set to false then it:

  • Sets the data-disabled of both to "true"
  • Calls a function called dialog like this: dialog(this).

I also don't want the click to event to work.

$('a[data-disabled]').click(function(e) {
  if (!$(this).data('disabled')) {
    e.preventDefault();
    $(this).attr('data-disabled', 'true');
    dialog(this);
  }
});

This should work:

$("a.accessLink").click(function(e){
    e.preventDefault();    
    if($(this).data("disabled") == false) {
        $("a.accessLink").data("disabled", true);
        dialog(this);
    }
});​​

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM