javascript 一些用法小结
如下的用法需要在JSP中增加属性onclick. 以全选为例,onclick="javascript:allselect()"//javascript可以省略
或者将function allselect()用document.getElementById("allselectbox").onclick = function();
全选:
fuction allselect(){
var form = document.forms[0];
var state = form.allselectbox.checked;
var length = form.username.length;
if(length){
for(var i=0;i<length;i++){
form.usernames[i].checked=state;
}
}else form.username.check = state;
}
全选的另一种写法:
function allSelect(){
var items = document.getElementsByName("usernames");
var state = document.getElementsByName("allselectbox")[0].checked;
if(items.length){
for(var i = 0; i < items.length; i++){
items[i].checked = state;
}
}else{
items.checked = state;
}
}
单项选择:
fuction selectitem(){
var form = document.forms[0];
var length = form.username.length;
if(length){
for(var i=0;i<length;i++){
if(form.usernames[i].checked) return true;
}
}else return form.usernames.check;
}
转到其他页面:
function topage(page){
var form = document.forms[0]
form.page.value = page;
form.submit;
}
取单选框的值Radio:
function getradiovalue(objradio){
var result = "";
try{
if(typeof(objradio.value)=="undefined"){
for(var i=0;i<objradio.length;i++){
if(objradio[i].checked){
return objradio[i].value;
}
}
}else{
if(objradio.checked) result = objradio.value;
}
}catch(e){result = "";}
return result;
}
email验证:
function isValidEmail(inEmail){
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return filter.test(inEmail);
}
检验手机格式:
function validateMobile(){
var form = document.forms[0];
if(form.mobile.value!=""){
var num = /^1[35]\d{9}$/
if(!num.test(form.mobile.value)){
alert("收货人的手机号格式不正确");
return false;
}
alert("正确");
}
}
待续........