【问题标题】:How to convert this observe_field to jquery如何将此observe_field转换为jquery
【发布时间】:2011-03-27 06:58:25
【问题描述】:

我想使用 jquery 而不是原型,我有点迷失在 jquery 中转换这个 observe_field。

<%=text_field_tag :section %>
<%= observe_field(:section, 
                  :frequency => 0.1, 
                  :update => "article_list",
                  :with => 'section', 
                  :url =>{ :action => :get_article_list }) %>

这是我的开始:

$(document).ready(function() {
  $("#section").bind("keyup", function() {
    var url = '/catalogs/get_article_list';
    $.get(url, function(html) {
      $("#article_list").html(html);
    });
  });
});

我读到了post,但我认为我遗漏了一些东西。

不胜感激。

谢谢。

【问题讨论】:

    标签: javascript jquery ruby-on-rails prototypejs


    【解决方案1】:

    观察者,观察任何输入元素上的事件,当事件发生时,它使用 ajax 向给定 url 发送请求,并在给定元素的页面上更新响应。

    这里你已经很好地观察到了事件,所以你已经编写了绑定section元素的keyup事件的代码。

    现在,当 keyup 事件触发时,您需要向服务器发出 ajax 请求。

     $(document).ready(function() {
       $("#section").bind("keyup", function() {
       data = {this.name: this.val()}; // the value of input that need to send to server
       $.get(url, data, // make ajax request
         function(html) { // function to handle the response
          $("#article_list").html(html); // change the inner html of update div
         });
       });
     });
    

    希望这能帮助你理解它。

    【讨论】:

    • 谢谢 Naren,这真的很清楚,它几乎可以工作但还没有:) 如何 data = {this.name: this.val()};知道这是应该发送到服务器的“部分”值吗?
    • 知道了! var data = {section:$(this).val()};再次感谢您的热心帮助!
    • @naren,如果我这样做,那么在我的网页中,表单没有呈现,但整个原始 html 正在呈现,如何解决这个问题?谢谢:)
    【解决方案2】:

    这也是一个很好的解决方案:https://github.com/splendeo/jquery.observe_field

    【讨论】:

      【解决方案3】:
      $(function() {
          // Executes a callback detecting changes
          $("#section"").change(function(){
          jQuery.ajax({data:'name=' + this.value, success:function(request){jQuery('#article_list').html(request);}, url:'/catalogs/get_article_list'
        })});});
      

      【讨论】:

        猜你喜欢
        • 2021-06-24
        • 1970-01-01
        • 2011-03-30
        • 2020-07-24
        • 2020-05-07
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多