
document.getElementById('hidetext').value="赋值"//js赋值
$("#hidetext").val("赋值")//jquery写法.
以上是html控件赋值方法
<input id="hidetext" type="hidden" runat="server"/>
hidetext.Text ="赋值"//cs后台复制
<asp:HiddenField ID="HiddenField1" runat="server" />
hidetext.value="赋值"//cs后台复制
以上是服务器端控件赋值方法(服务器端也可以使用js,jquery进行赋值)
方法1<html>
<head>
<title>把input file类型的value清空--Test by 编程浪子</title>
<script>
function clearMethod1()
{
var objFile=document.getElementsByTagName('input')[0]
alert("\""+objFile.value+"\"已清除")
objFile.outerHTML=objFile.outerHTML.replace(/(value=\").+\"/i,"$1\"")
}
function clearMethod2()
{
var objFile=document.getElementsByTagName('input')[0]
alert("\""+objFile.value+"\"已清除")
var WshShell=new ActiveXObject("WScript.Shell")
objFile.focus()
objFile.createTextRange().select()
WshShell.SendKeys("{del}")
}
function clearMethod3()
{
var objFile=document.getElementsByTagName('input')[1]
alert("\""+objFile.value+"\"已清除")
objFile.value=""
}
</script>
<head>
<body>
<input type="file" value="C:\abc.txt"/><br><br>
<button onclick="clearMethod1()">清除大法1:李代桃僵</button><br>
<button onclick="clearMethod2()">清除大法2:暗渡陈仓</button>
<br><br><br><br><br><br><br>
<input type="text" value=""/><input type="file"
value="C:\abc.txt" onchange="this.previousSibling.value=this.value"
style="width:20"/><br><br>
<button onclick="clearMethod3()">清除大法3:声东击西</button>
</body>
<html>
方法2
<html>
<head>
<title>input file赋初值--Test by 编程浪子</title>
<script>
function getInitVal()
{
var objFile=document.getElementsByTagName('input')[0]
var WshShell=new ActiveXObject("WScript.Shell")
objFile.focus()
WshShell.SendKeys("C:\\abc.txt")
}
</script>
<head>
<body onload="getInitVal()">
<input type="file"/>(调到ActiveX)
<br><br><br><br>
<input type="text" value="C:\abc.txt"/><input type="file" style="width:20"
onchange="this.previousSibling.value=this.value"/>(直接模拟)
</body>
<html>
不可以,必须用文件对话框选择。处于安全的考虑,不能直接对input type=file赋值,试想一下:如果某个网站在网页中嵌入了一个隐藏的input type=file,他可以直接用JS将你硬盘中的某个文件通过该控件直接取到指定地点,那还有什么安全可言?input type=file设置一个按钮来打开文件选择框的目的就是选择文件必须让用户知道并且决定是否要选择一个文件,所以赋值是没有作用的,你要赋值可以写 inputID.value = "xxxx.xxx ",当然,这是没有作用的。欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)