如何将JavaScript添加到输入字段
我在Rails 3.1中有这个输入字段:如何将JavaScript添加到输入字段,javascript,ruby-on-rails,ruby-on-rails-3.1,Javascript,Ruby On Rails,Ruby On Rails 3.1,我在Rails 3.1中有这个输入字段: <%=f.file_field :image, :id => "upload"%> 我该怎么做 必须在HTML中看到以下内容: <input type="file" onchange="readURL(this); name="post[image]" id="upload"> 您正在使用JQuery吗 $("#upload").change(function() { .. }); 是单向的 $("#uploa
<%=f.file_field :image, :id => "upload"%>
我该怎么做
必须在HTML中看到以下内容:
<input type="file" onchange="readURL(this); name="post[image]" id="upload">
您正在使用JQuery吗
$("#upload").change(function() {
..
});
是单向的
$("#upload").bind("change", function() {
..
});
这是另一个
更新:
你可以这样写:
<%= f.file_field :image, :id => "upload", :onchange => "readURL(this)" %>
“上传”,:onchange=>“读取URL(此)”%>
这将为您提供类似以下内容的HTML:
<input type="file" onchange="readURL(this); return false;" name="post[image]" id="upload">
实现这一点的首选方法是通过不引人注目的JavaScript(本质上是在加载DOM后向页面添加JavaScript功能,并使其成为可选的)。在rails 3.1和更高版本中,您希望在Syed对app/assets/javascript/controller_name.JS.coffee
或类似的回答中添加JS代码。这很好,但是我如何才能在jquery代码中添加onchange=“readURL(this);
?谢谢你,Kyle,我将使用不显眼的javascript:D
<input type="file" onchange="readURL(this); return false;" name="post[image]" id="upload">