Javascript和Java获取各种form表单信息的简单实例_javascript技巧
程序员文章站
2022-05-17 21:49:36
...
大家都知道我们在提交form的时候用了多种input表单。可是不是每一种input表单都是很简单的用Document.getElementById的方式就可以获取到的。有一些组合的form类似于checkbox或者radio或者select我们如何用javascript获取和在服务器中获取提交过来的参数呢?多说无用、上代码:
Javascript:
function check(){
var radio = document.getElementsByName("xingbie");
var checkbox = document.getElementsByName("hobby");
var select = document.getElementById("opt");
//获取select标签
var index = select.selectedIndex;
var text = select.options[index].text;
var value = select.options[index].value;
//获取radio标签
for(var i=0;i if(xingbie.item(i).checked){
var val = xingbie.item(i).getAttribute("value");
break;
}
continue;
}
//获取checkbox标签
for(var i=0;i if(hobbys[i].checked){
alert(hobbys[i].value);
}
continue;
}
//提交form表单
document.formkk.submit();
}
Java:
String[] hobbys = request.getParameterValues("hobby"); //checkbox
String text = request.getParameter("text"); //text
String password = request.getParameter("password"); //password
String xingbie = request.getParameter("xingbie"); //radio
request.getParameter("hidden");
request.getParameter("opt"); //select
Jsp-html代码:
复制代码 代码如下:
Javascript:
复制代码 代码如下:
function check(){
var radio = document.getElementsByName("xingbie");
var checkbox = document.getElementsByName("hobby");
var select = document.getElementById("opt");
//获取select标签
var index = select.selectedIndex;
var text = select.options[index].text;
var value = select.options[index].value;
//获取radio标签
for(var i=0;i
var val = xingbie.item(i).getAttribute("value");
break;
}
continue;
}
//获取checkbox标签
for(var i=0;i
alert(hobbys[i].value);
}
continue;
}
//提交form表单
document.formkk.submit();
}
Java:
复制代码 代码如下:
String[] hobbys = request.getParameterValues("hobby"); //checkbox
String text = request.getParameter("text"); //text
String password = request.getParameter("password"); //password
String xingbie = request.getParameter("xingbie"); //radio
request.getParameter("hidden");
request.getParameter("opt"); //select
上一篇: vueJs如何实现图片轮播的实例代码分享