Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将JavaScript添加到输入字段_Javascript_Ruby On Rails_Ruby On Rails 3.1 - Fatal编程技术网

如何将JavaScript添加到输入字段

如何将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

我在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() { 
  ..
}); 
是单向的

$("#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">