0

进行动态选择时出现错误。当我更改我的选择时,我的 ajax 不起作用。

这是视图代码:

<%= select_tag 'cost', options_for_select(Cost.all.map { |c| ["#{c.types}", c.id] }),:id => "cost_select" %>

<script type="text/javascript">
$(document).ready(function(){
    $("#cost_select").live('change',function(){
      $.ajax({
        url: "finances/cost_type",
        type: "GET",
        data: {'cost=' + $('#cost_select option:selected').value() },
      })
    });
});
</script>

我是我的财务总监,我有这个:

def cost_type
  @places = Place.find_by_cost_id(params[:cost])
  respond_with @places
end

我制作了cost_type.js.erb文件,我的回复是在 js 中。

我的问题是,当我更改选择标签时没有激活任何操作,我的搜索中的代码问题如何,我发现此代码有效,但问题是什么?

更新:

我的 routes.rb 包含:

get "finances/cost_type" => "finances#cost_type"

我用以下方法解决了我原来的问题:

$(document).ready(function(){
    $("#cost_select").change(function(){
      $.ajax({
        url: "finances/cost_type",
        type: "GET",
        data: {cost: $("#cost_select option:selected").val() },
      })
    });
});

有了这个,我收到了一个有效的回复(我通过萤火虫验证了):

$("#cost_select").after("<select id="from_money" name="from_money"><option value="2">Luz</option></select>
");

但是,数据没有出现在我的视图中。我试着把它放在不同的地方<div>,但<select>不会出现。

我不知道是什么问题,请给我任何可能的建议。

4

1 回答 1

0

将您的 javascript 更改为

$(document).ready(function(){
  $("#cost_select").live('change',function(){
    $.ajax({
      url: "/finances/cost_type",
      type: "GET",
      data: { cost: $('#cost_select').val() }
    })
  });
});

财务之前的额外费用/确保您使用的是绝对路径。data正如其中一条评论指出的那样,该部分也是错误的,但这是一个更简洁的版本。

于 2013-02-08T02:24:37.690 回答