【问题标题】:Appear Select dynamic on change other select Rails出现 Select dynamic on change other select Rails
【发布时间】:2013-01-23 08:32:07
【问题描述】:

我在进行动态选择时出错。当我更改我的选择时,我的 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() },
      })
    });
});

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

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

但是,数据并未出现在我的视图中。我尝试将其放入不同的&lt;div&gt;,但&lt;select&gt; 不会出现。

我不知道哪里出了问题,请给我任何可能的建议。

【问题讨论】:

  • 尝试在你的ajax中使用data: {cost : $('#cost_select option:selected').value() }
  • 无效,不改变事件,不出现任何内容,我更新了 routes.rb
  • 您在行动中获得了成本的价值吗?
  • 当我选择我希望 ajax 不将成本发送到控制器时,当我更改时没有任何活动我查看控制台并没有出现任何内容,我检查我的 javascript 我认为是这个,你的问题我不明白对不起。

标签: ruby-on-rails ajax ruby-on-rails-3 jquery ruby-on-rails-3.1


【解决方案1】:

把你的javascript改成

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-09
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多