无法在Jquery方法中获取对象(项)的Id

时间:2014-12-05 07:30:38

标签: jquery ruby-on-rails

当我运行此代码警报时显示未查找。 但如果我试图让其他成员正确显示它。 即alert(data [row] [' name'])将显示正确的名称。



//this is the jquery method 

$('#search').keyup(function() {
      var textValue = $(this).val();
      $.get('/items', {
        search: textValue
      }, function(data) {
        for (row in data) {
          alert(data[row]['id']);
        }
      });
    });

# This is my Rails ItemController. 

class ItemsController < ApplicationController 
  def index 
    if request.xhr?
       @items=Item.search(params[:search]) 
       render:json=>@items
     else
       @items=Item.all

      end 
    end 
end
&#13;
// this is the search form in index.html.erb page

<form id="item-search">
  <input type="text" id="search" name="query" placeholder="Type to search">
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

请改为使用jQuery.get代替:

$('#search').keyup(function() {
  var textValue = $(this).val();
  $.get('/items', { search: textValue })
    .done(function(data) {
      for (row in data) {
        alert(data[row]['id']);
      }
    });
});