将自己在web开发中经常用到的一些JS总结一下。
1.修改标签和表单的值
修改标签的值:
var customer = document.getElementById("custm" ).innerHTML;
alert(customer);
document.getElementById( "custm").innerHTML = "name"
修改控件(input)的值:
var userName = document.getElementById("userName");
alert(userName.value);
userName.value = "new value" ;
2.js提交form表单
function updateInfo(formName) {
var form = document.getElementById(formName);
form.submit();
}
3.自定义html中file控件
<input type= "file" id ="fileupload" name= "fileupload" style="display :none"/>
<input type= "button" onclick ="fileupload.click()" id="uploadFileButton" value="浏 览 ..." class ="btn"/>
<input type="submit" name= "submit" value ="上传" onclick="return ajaxFileUpload();" class="btn">
4.实现多个复选框的多选和单选
全选
function checkAll(checkall_id, checkName) {
var checkArr = document.getElementsByName(checkName);
var checkAll = document.getElementById(checkall_id);
if(checkAll.checked == true ) {
for (var i=0; i<checkArr.length; i++) {
checkArr[i].checked = true ;
}
} else {
for (var i=0; i<checkArr.length; i++) {
checkArr[i].checked = false ;
}
}
}
单选
function checkSingle(single_id, checkName) {
var checkArr = document.getElementsByName(checkName);
var checkSingle = document.getElementById(single_id);
for( var i=0; i<checkArr.length; i++) {
if (checkArr[i].value != single_id) {
checkArr[i].checked = false ;
} else {
checkArr[i].checked = true ;
}
}
}
4.在加载页面的时候调用js方法
<script type="text/javascript">
window.onload=function() //用window的onload事件,窗体加载完毕的时候
{
//do something
}
</script>