Rails 在选择元素更改时更新表单内容

发布于 2024-10-22 20:06:17 字数 520 浏览 1 评论 0原文

我有一个带有选择框和三个文本输入框的表单。当选择框中的选项发生变化时,我想用与所选选项相对应的值更新三个文本输入框。这些值是存储在服务器上的模型对象。

我正在使用 Rails 3 并试图保持低调,但我找不到一个干净的方法来做到这一点。我想出了几个可怕的、混乱的解决方案,但还没有一个干净的解决方案。将这一切联系在一起的好方法是什么?特别是,我在选择元素从服务器请求模型对象时遇到问题。

这是生成表单的视图:

= form.select :region_id, region_options_for_select(@business_location.region), :prompt => '-- Region --'
= form.text_field :city
= form.text_field :state, :label => 'Province/State'
= form.text_field :country

所以我希望每当“地区”更改时填充城市/州/国家字段。请注意,区域也是表单本身的一部分。

I have a form with a select box and three text input boxes. When the option in the select box changes, I want to update the three text input boxes with values corresponding to the selected option. The values are a model object stored on the server.

I'm using Rails 3 and trying to keep things unobstrusive, but I can't find a clean way to do this. I've come up with a couple horrendous, messy solutions, but nothing clean yet. What would be a good way to tie this all together? In particular, I am having trouble with the select element requesting the model object from the server.

Here's the view that generates the form:

= form.select :region_id, region_options_for_select(@business_location.region), :prompt => '-- Region --'
= form.text_field :city
= form.text_field :state, :label => 'Province/State'
= form.text_field :country

So I wish to populate the city/state/country fields whenever "region" is changed. Note that region is also part of the form itself.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

靖瑶 2024-10-29 20:06:17

在 jQuery 中,类似这样的东西可以工作:

$("select").live("change", function(e) {
  e.preventDefault();
  $.ajax({
    url: "/location/" + $(this).val(), //I think, it could be something else
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
      //you're returning a JSON object, so just iterate through it and bind it to
      //the value attributes of each field
    },
    error: function(xhr,exception,status) {
      //catch any errors here
    }
  });
});

在 Rails 端,在 LocationsController#show 中:

@location = Location.find(params[:id])
respond_to do |format|
  format.json { render :json => @location.to_json, :status => 200 }
end

大致就是这样工作的。将您拥有的任何控制器/模型名称替换为我编写的名称。

In jQuery, something like this could work:

$("select").live("change", function(e) {
  e.preventDefault();
  $.ajax({
    url: "/location/" + $(this).val(), //I think, it could be something else
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
      //you're returning a JSON object, so just iterate through it and bind it to
      //the value attributes of each field
    },
    error: function(xhr,exception,status) {
      //catch any errors here
    }
  });
});

On the Rails side, in the LocationsController#show:

@location = Location.find(params[:id])
respond_to do |format|
  format.json { render :json => @location.to_json, :status => 200 }
end

That's roughly how it could work. Substitute whatever the controller/model names you have for the ones I made up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文