I'm having problems using event.preventDefault (); for ahref on my DOM

I'm having trouble using event.preventDefault();for ahrefin my DOM.

How do you disable the nofollow delete publication url as specified in HTML using jQuery?

<td class="trash_can">
<a rel="nofollow" data-remote="true" data-method="delete" data-confirm="Are you sure you want to delete Greek Theater at U.C. Berkeley?" href="/promotions/2/places/46">
<img id="trash_can" src="http://test.dev/images/trash.png?1305741883" alt="Trash">

The following code should undermine the HTML, but the HTML sends the delete action before giving the warning "are you sure." nothing works:

  $(function(){
  $('.trash_can a').click(function(event) {
    event.preventDefault();
    console.log('Clicked Delete');
  });
});
+5
source share
3 answers

, event.preventDefault() return false. preventDefault() , , , , , :

 $(rails.linkClickSelector).live('click.rails', function(e) {
    var link = $(this);
    if (!rails.allowAction(link)) return rails.stopEverything(e);

    if (link.data('remote') !== undefined) {
      rails.handleRemote(link);
      return false;
    } else if (link.data('method')) {
      rails.handleMethod(link);
      return false;
    }
  });
+7

? .

event.preventDefault() , . Rails , "", Rails. , , .

JQuery, Rails:

https://github.com/rails/jquery-ujs/blob/master/src/rails.js

( ):

linkClickSelector: 'a[data-confirm], a[data-method], a[data-remote]'

, , data-remote, data-method data-confirm.

, , .

+4

, , . , , - :

function enableLink() {
  jQuery("#link_id").removeClass("disabled");
  jQuery("#link_id").attr("data-remote", true);
}


function disableLink() {
  jQuery("#link_id").addClass("disabled");
  jQuery("#link_id").removeAttr("data-remote");
}

jQuery(document).ready(function(){
  jQuery(document).on("click", "disabled", function(e){
    e.preventDefault();
  });
});

. .

0

All Articles