简体   繁体   English

Ajax在输入字段中提交

[英]Ajax submit in input field

I know this may be a common question but I've been searching for a couple hours with no luck, so I figured I'd ask. 我知道这可能是一个常见问题,但是我一直在找几个小时没有运气,所以我想问一下。

How do make a form submit through ajax on an input field change? 如何通过输入字段更改通过ajax提交表单? I have a form where, among other things, a user selects a product from a dropdown, and then I want that product's picture to show immediately through ajax. 我有一个表单,其中除其他外,用户从下拉菜单中选择一种产品,然后我希望该产品的图片立即通过ajax显示。 This is what I have: 这就是我所拥有的:

<%= f.association :product, collection: @products %>
<div id="prodPicture">...</div>

What I think might be the right way is adding remote: true and the url but I haven't gotten it to work as of yet. 我认为可能正确的方法是添加远程:true和url,但到目前为止我还没有使用它。

<%= f.association :product, collection: @products, remote: true, url: url_for(controller: 'quotes', action: 'getdata'), type: 'JSON' %>

Is that the correct way to adding an ajax event to a form field change? 这是将ajax事件添加到表单字段更改的正确方法吗?

Thanks in advance for any help! 在此先感谢您的帮助!

you bind to the dropdowns change event 您绑定到下拉菜单更改事件

$("#idofdropdown").change(function(){
var selection = $(this).val() // always gets selected value by default
$.ajax()//and so forth

});

you can create hidden img element on page and then add some JS code: ("#idofdropdown").change(function(){ $('#id_image').attr('src', PRODUCTS_IMAGES[parseInt($(this).val())] }); 您可以在页面上创建隐藏的img元素,然后添加一些JS代码:( ("#idofdropdown").change(function(){ $('#id_image').attr('src', PRODUCTS_IMAGES[parseInt($(this).val())] });

then show img. 然后显示img。

where var PRODUCTS_IMAGES = #{@products.collect{|e| [e.id, e.file.url]}.to_json}; 其中var PRODUCTS_IMAGES = #{@products.collect{|e| [e.id, e.file.url]}.to_json}; var PRODUCTS_IMAGES = #{@products.collect{|e| [e.id, e.file.url]}.to_json};

something like that....basically you have a JS dictionary with selected ID in dropdown and img src. 基本上是这样。...基本上,您有一个JS字典,在下拉列表和img src中具有选定的ID。

Do you want to when you change your collection and display the picture immediately? 您要更改收藏并立即显示图片吗?

You can do this in two way: 您可以通过两种方式执行此操作:

1. Add data-image-url to option 1.将data-image-url添加到选项

The best way is image url add into the option use data-image-url : 最好的方法是将图像url添加到使用data-image-url的选项中:

<%= f.association :product, include_blank: false, label: 'Product', input_html: {class: 'product_select' do %>
  <%= f.select :product, @products.map{|a| [a.name, a.id, {"data-image-url" => a.image_url}]} %>
<% end %>

And use this javascript: 并使用以下javascript:

<script type="text/javascript">
  $(".product_select").change(function(){
    var selection = $(this).find(':selected').data("imageUrl");   //The product image url
    $('#prodPicture').html("<img src='" + selection + "' />")
  });
</script>

2. Use ajax 2.使用ajax

The ajax javascript use post: ajax javascript使用后:

<script type="text/javascript">
  $(".product_select").change(function(){
    var selection = $(this).val();
    $.post("/products/return_image_url",
            {product_id: selection},
            function(data) {
               if (data != null) {
                  $('#prodPicture').html("<img src='" + data + "' />")
               }
             }
           )
  });
</script>

And the ruby action about this: 和红宝石的行动有关:

def return_image_url
  product = Product.find(params[:product_id])
  ...
  respond_to do |format|
    format.json { render :json => product.image_url }
  end
end

And I think you do better use Add data-image-url to option . 而且我认为您最好使用Add data-image-url to option

I want to write something about remote: true : 我想写一些关于remote: true

remote: true came from jquery_ujs , and it can use in the link_to , button_to or submit . remote: true来自jquery_ujs ,它可以在使用link_tobutton_tosubmit It must have a link. 它必须有一个链接。 When you change the select or radio and so on, could not use remote: true . 当您更改选择或收音机等时,不能使用remote: true

You can learn more about remote: true in the guides: http://guides.rubyonrails.org/working_with_javascript_in_rails.html 您可以在指南中了解有关remote: true更多信息: http : //guides.rubyonrails.org/working_with_javascript_in_rails.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM