[英]How can I get my javascript work in an asp.net cshtml file?
I have a html input for uploading a picture to my ftp, and I want my JS the detect changes on my button. 我有一个用于将图片上传到ftp的html输入,并且我希望JS检测按钮上的更改。 My cshtml is: 我的cshtml是:
@using (Html.BeginForm("UploadFile", "Upload", FormMethod.Post, new {
enctype = "multipart/form-data" }))
{
<input id="uploadFile" placeholder="No file..." disabled="disabled" />
<div class="fileUpload btn btn-primary">
<span>Browse</span>
<input id="uploadBtn" type="file" class="upload" />
</div>
<input type="submit" title="OK" />
}
and my script is 而我的剧本是
<head>
<script type="text/javascript" language="javascript">
document.getElementById("uploadBtn").onchange = function ()
{
document.getElementById("uploadFile").value = this.value;
};
</script>
</head>
When I run my code I get the following message: 运行代码时,我收到以下消息:
0x800a138f - JavaScript runtime error: Unable to get property 'onchange' of undefined or null reference 0x800a138f-JavaScript运行时错误:无法获取未定义或空引用的属性“ onchange”
I checked my html and JS in fiddle, everything works great, so I guess that Razor and JS have some problems with each others. 我检查了html和JS,一切正常,所以我猜Razor和JS彼此之间有一些问题。 Any tips? 有小费吗?
Try this: 尝试这个:
1. 1。
<head>
<script type="text/javascript" language="javascript">
$(document).ready(function(){
document.getElementById("uploadBtn").onchange = function ()
{
document.getElementById("uploadFile").value = this.value;
};
});
</script>
</head>
As per @StephenMuecke : Move your script to immediately before the closing </body>
tag (as my view it's best way) 根据@StephenMuecke :将脚本移至</body>
标记之前(据我所知,这是最好的方法)
<body> .............. .............
<script type="text/javascript" language="javascript"> $(document).ready(function(){ $('#uploadBtn').on('change', function() { $("#uploadFile").val($(this).val()); }) }); </script>
</body>
3.If you use separate script ( .js
file) then try 3.如果使用单独的脚本( .js
文件),请尝试
$(document).on('change','#uploadBtn' ,function() {
$("#uploadFile").val($(this).val());
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.