这是我想要实现的一件小事。我有一个asp.net FileUpload和一个文本框。当用户单击fileUpload并从他/她的计算机/设备中选择图片时,我希望在提交之前将图像名称立即显示在文本框中。这是我试过的
<asp:FileUpload ID="Upload" runat="server" ClientIDMode="Static" />
<asp:TextBox ID="txtImage" runat="server" ClientIDMode="Static">
$('#Upload').change(function () {
var filename = $(this).val();
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex > 0) {
filename = filename.substring(lastIndex + 1);
}
$('txtImage').val(filename);
});
仍然无法显示它。我很想听见
答案 0 :(得分:0)
#
中您遗失了$("txtImage")
。这应该是这样的:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
console.log("ready!");
$('#Upload').change(function () {
var filename = $(this).val();
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex > 0) {
filename = filename.substring(lastIndex + 1);
}
$('#txtImage').val(filename);
});
});
</script>
<asp:FileUpload ID="Upload" runat="server" ClientIDMode="Static" />
<asp:TextBox ID="txtImage" runat="server" ClientIDMode="Static"></asp:TextBox>
答案 1 :(得分:0)
TextBox txtImage没有结束标记。
<asp:TextBox ID="txtImage" runat="server" ClientIDMode="Static"/>