8

jRailsなしでjQueryを使用したRails/Prototypeのobserve_fieldメソッドに相当するものはありますか?

will_paginate を使用して、入力どおりに検索しています。

<%= observe_field('検索',
                  :頻度 => 2,
                  :update => '結果',
                  :loading => "Element.show('spinner')",
                  :complete => "Element.hide('spinner')",
                  :url => { :controller => 'foo', :action => 'search' },
                  :with => "'検索=' + エスケープ(値)") %>

4

2 に答える 2

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
  });
});

コメントへの返信

キーアップに反応したい場合は、最初の行を次のように置き換えます

$("#search").bind("keyup", // etc...
于 2010-01-24T15:51:40.687 に答える
7

これを試して:

https://github.com/splendeo/jquery.observe_field

于 2011-03-08T00:58:40.950 に答える