سؤال

I am listening to a button and some a tags with this code:

$("#pagination_partial a, #order_sorting_partial a, #order_sorting_partial .organize-btn").bind("click", function() {
  $.getScript(this.href);
  history.pushState(null, "", this.href);
  return false;
});

The code is supposed to get the href from the clicked element, but as of now it is only getting the href from the a tags. When I click the organize-btn the code understands that the button is clicked, but it does not assign anything to @href.

What am I doing wrong? Here is the view file (HAML):

.row
    .span
        .btn-group
            %button.btn{:href => "/orders?view=#{@organize_mode}", :class => "organize-btn"}= t(".#{@organize_mode}")
            %button.btn.dropdown-toggle{"data-toggle" => "dropdown"}
                %span.caret
            %ul.dropdown-menu
                %li
                    %a{:href => "/orders?view=by_last_dispatch"}= t(".by_last_dispatch")
                %li
                    %a{:href => "/orders?view=by_first_dispatch"}= t(".by_first_dispatch")
هل كانت مفيدة؟

المحلول

The js object form of an anchor tag happens to have a href property, but don't confuse that as the same thing as having an href attribute on the DOM tag. In your markup you are adding an href attribute to your buttons, but those don't get automatically added to the js object like you are expecting. Assuming you want to continue to use an attribute named href, which I consider to be another off topic question, you will need to access that attribute from the DOM it self. With jQuery, you would want to replace your this.href with $(this).attr("href")

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top