form表单的JS交互
2016-12-06 本文已影响0人
瓦是香蕉妹
读取表单的值
function showFormValue(){
var form = document.forms["myform"];
alert(form.name.value);
alert(form.xb.value);
//debugger;断点检查
var arr = form.ah;
var sel = [];
for(var i=0;i<arr.length;i++){
if (arr[i].checked==true){
sel.push(arr[i].value);
}
}
alert(sel.join(","));
var jn = form.jn.value;
alert(jn);
var grjj = form.grjj.value;
alert(grjj);
var hid = form.hid.value;
alert(hid);
alert(document.getElementById("xm".value));
}
function setFormValue(){
var form = document.forms["myform"];
form.name.value = "林";
form.jn.value = 2;
form.grjj.value = "请输入个人简介";
form.xb.value = 2;
var ahs = form.ah;
var len = ahs.length;
for (var i=0;i<len;i++){
if(i==0){
continue;
}
if(i==len-1){
break;
}
ahs[i].checked = true;
}
}
给按钮注册一个事件
window.onload = function(){
//获取按钮对象
var subBtn = document.getElementById("subBtn");
subBtn.onclick = showFormValue;
setFormValue();
}