javascript 一些用法小結

來源:互聯網
上載者:User

如下的用法需要在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("正確");
}
}
待續........
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.