简体   繁体   English

在Asp.net MVC 4中使用javascript / JQuery浏览并设置img src

[英]Browse and set img src using javascript/JQuery in Asp.net mvc 4

I have a <img/> tag in my .cshtml page.. 我的.cshtml页面中有一个<img/>标签。

<img id="ImageID" alt="" width="100%" height="100%" />

I need to set src attribute dynamically by selecting the image file path using file control. 我需要通过使用文件控件选择图像文件路径来动态设置src属性。

I tried the following code in my Js page, but it is not working. 我在Js页面中尝试了以下代码,但无法正常工作。

var image_path = $("#fileControl").val();

$("#ImageID").attr('src', 'url(' + image_path + ')');

Please help! 请帮忙!

Thanks. 谢谢。

To change it to a live event, so that it changes as when your #fileControl value changes, do: 要将其更改为实时事件,以使其在#fileControl值更改时更改,请执行以下操作:

$("#fileControl").on('change', function(){
  $("#ImageID").attr('src', 'url(file://' + $(this).val() + ')');
})

...but obviously the above may change slightly (on, bind, live), depending on you jQuery version ...但显然以上内容可能会略有变化(启用,绑定,实时),具体取决于您的jQuery版本

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

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