是否使用不带jRails的jQuery的Rails / Prototype observe_field方法等效?
我正在使用will_paginate进行搜索:
<%= observe_field('search', :frequency => 2, :update => 'results', :loading => "Element.show('spinner')", :complete => "Element.hide('spinner')", :url => { :controller => 'foo', :action => 'search' }, :with => "'search=' + escape(value)") %>
答案 0 :(得分:10)
使用jQuery,你需要使用一个选择器,这样的东西应该可以工作
$("#search").bind("blur", function() {
$("#spinner").show(); // show the spinner
var form = $(this).parents("form"); // grab the form wrapping the search bar.
var url = form.attr("action"); // grab the URL from the form's action value.
var formData = form.serialize(); // grab the data in the form
$.get(url, formData, function(html) { // perform an AJAX get, the trailing function is what happens on successful get.
$("#spinner").hide(); // hide the spinner
$("#results").html(html); // replace the "results" div with the result of action taken
});
});
对评论的回应
如果您想对keyup做出反应,请用
替换第一行$("#search").bind("keyup", // etc...
答案 1 :(得分:7)