English 中文(简体)
鲁比拉铁路——联邦慈善公司正在被处决,但没有发出亚克斯呼吁
原标题:Ruby rails - jQuery JS being executed but not making ajax call

想弄清为什么这种“优质联合材料”在呼吁执行时发出“雅克斯”呼吁。

I have this button to make an ajax GET request to a method in the controller, the method will partial render. When I click on the button I don t see any request coming on the console but I see the alert "test" on the browser.
I have the same exact JS with other parameters working for other tupes of ajax calls, so I just copied one of them and changed all required parameters, expecting it shall work right away. Neither I get any errors on the console. My routes and id names are good and verified. What is it that I am missing here?

意见-说明:本纽特通过另一旁系提供,该部分工程使用。

<%= button_tag "Add / Remove", :id => "add_remove_button", :onclick => "javascript:add_remove();" %> #note: this is buried under div tags in a html table

JS-

 function add_remove(){
$( #add_remove_button ).click(function() {
      $.ajax({
        type:  GET ,
        url: "/item/add_remove", 
        success:$( #view_item ).html(data)

        /*function(){  },
        data:$( #test ).serialize(),
        error: function(){  },
        success: function(data){   },
        complete: function (){   }*/
        }); #No ajax call made
/*return false;*/
});
alert( test ); #I get this alert
}
最佳回答

You ll always see that alert() because click() is asynchronous: the code inside the function() passed to click does not get executed until you click, but the rest of add_remove() will get called.

这里指的是贵国法典中实际发生的情况,这解释了为什么美国退休人员协会的呼吁没有做到:

  1. Using :onclick => ... attaches add_remove() to your button.
  2. You click the button, add_remove() gets called and attaches another click callback to your button. Then add_remove() calls alert(). There is no AJAX call happening here, just adding a new click handler, and sending an alert.
  3. You click the button a second time, and you will attach a third click callback to the button. However since you also attached a click handler the first time you clicked the button, you should see an AJAX request here.
  4. Click it a third time and you ll see two AJAX requests this time, for a total of 3 AJAX requests.

这里是你实际想要做的事。 移除:onclick => ......从纽芬兰州移走:

<%= button_tag "Add / Remove", :id => "add_remove_button" %>

在网页上首批装载时,向纽芬兰州照射一个点击事件:

$(function(){
  $( #add_remove_button ).click(function() {
    $.ajax({
      type:  GET ,
      url: "/item/add_remove", 
      success: function(data) { $( #view_item ).html(data); },
      data: $( #test ).serialize(),
      error: function(){  },
      success: function(data){   },
      complete: function (){   }
    });
    return false;
  });
});
问题回答

You are mixing up the jquery-registering-callbacks style and the old event-handler attributes. So you triggered the registering when calling the add_remove function.

简单删除:onclick 方英尺重, 功能添加_remove(){ ......}





相关问题
Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

rails collection_select vs. select

collection_select and select Rails helpers: Which one should I use? I can t see a difference in both ways. Both helpers take a collection and generates options tags inside a select tag. Is there a ...

RubyCAS-Client question: Rails

I ve installed RubyCAS-Client version 2.1.0 as a plugin within a rails app. It s working, but I d like to remove the ?ticket= in the url. Is this possible?

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

multiple ruby extension modules under one directory

Can sources for discrete ruby extension modules live in the same directory, controlled by the same extconf.rb script? Background: I ve a project with two extension modules, foo.so and bar.so which ...

Text Editor for Ruby-on-Rails

guys which text editor is good for Rubyonrails? i m using Windows and i was using E-Texteditor but its not free n its expired now can anyone plese tell me any free texteditor? n which one is best an ...

热门标签