玩转web之javaScript(五)---js和jquery一些不可不知的方法(input篇)

很多时候我们都利用js和jquery中操作input,比如追加属性,改变属性值等等,我在这里简单的整理了一下,并在以后逐步补充。


1:删除input的某一属性。
<input name="code" id="code" readonly="true"/>该input包含一个readonly属性,该属性表示该input是只读的,我们可以使用jquery的方法:$('#code').removeAttr("readonly");删除input的该属性使其可编辑。

2:修改input的某一属性的值
<input name="code" id="code" style="300px;height:20px" type="password"/>这是一个密码输入框,有时我们会需要在js中将其改为显示明码,js里可以使用此方法:       document.getElementById("code").type="text";
对于style中的属性,也可以改变,例如:document.getElementById("code").style.width="20px";

当然,也可以使用jquery的方法: $("code").attr("readOnly",true);

3:为input追加属性
对于<input name="code" id="code" style="300px;height:20px"> ,为其追加一个type属性:document.getElementById("code").type="password";使其变为密码输入框,同理:document.getElementById("code").readOnly=true;为其添加只读属性

4:判断input输入的值是否为数字--isNaN
例如:var x=document.getElementById("code").value;
if(x==""||isNaN(x))
{
alert("输入的不是数字");
}
}

[2014/5/19补充]

5:为input文本框赋值。

<input name="code" id="code" />

在js里为其赋值的方法:document.getElementById("code").value=“aaa”;

在jquery里为其赋值的方法: $("#code").val("aa");



版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/dingxiaoyue/p/4931800.html