600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > html单选框servlet js servlet分别获取下拉框 单选框 复选框的值

html单选框servlet js servlet分别获取下拉框 单选框 复选框的值

时间:2019-12-20 17:58:29

相关推荐

html单选框servlet js servlet分别获取下拉框 单选框 复选框的值

A 获取下拉框的value值

1.1 js获取下拉框中的value值从而给出提示:

jsp:

请选择收藏夹类型

${fa.faDesc}

js:

var faType = document.getElementById("faTypeSel").value;

if (faType.length == 0)

{

jAlert("无法提交!您未选择任何收藏夹类型","温馨提示");

return false;

}

有时候不同的浏览器可能不支持某些js代码,其他方法还有:

var faType = document.getElementById("faTypeSel");

var index = faType.options.selectedIndex;或者 var index = faType.selectedIndex;

var value = faType.options[index].value;

if (value.length == 0)

{

jAlert("无法提交!您未选择任何收藏夹类型","温馨提示");

return false;

}

1.2 servlet获取下拉框中的value值:

String faTypeSel = request.getParameter("faTypeSel");

B、获取单选框或者多选框的value值

2、js获取单选框或者多选框的value值:

jsp的form中:

说明:(如果是js提交的话就用button加上onclick();如果是form表单提交的方式就直接用submit,去掉onclick)

2.1 对应的js:

function joinVote()

{

var targetId = '';

var type = '';

var max= '';

var count = 0;

var ids="";

var items = document.getElementsByName("vote");

for(var i=0;i

{

if(items[i].checked)

{

count++;

ids+=items[i].value+",";

}

}

if( == type && count > max)

{

alert("提示:不能超过最多投票项数");

return;

}

if(ids=="")

{

alert("提示:嗨,您还没有选择投票项");

return;

}else

{

ids=ids.substring(0,ids.length-1);

window.location.href='/action? voteAction=0&action=join&voteId='+targetId+'&ids='+ids;

}

}

2.2、servlet中获取单选框或者多选框的value值:

String tipText = "";

String url = "";

String ids ="";

int count = 0;

String voteId = context.getRequest().getParameter("voteId");

String Is_multi = context.getRequest().getParameter("Is_multi");

int Choice_max = Integer.parseInt(context.getRequest().getParameter("Choice_max"));

String[] vote = context.getRequest().getParameterValues("vote");

StringBuffer buf = new StringBuffer();

if(vote !=null && vote.length >0)

{

for(int i=0;i

{

count++;

buf.append(vote[i].toString()+",");

}

if(WebConstants.VOTE_CHECK_TYPE_CHECKBOX == Is_multi && count > Choice_max)

{

tipText = " 不能超过最多投票项数!";

url = "/action?voteAction=0&action=detail&voteId="+voteId;

}

}

else

{

tipText = " 您还没有选择投票项!";

url = "/action?voteAction=0&action=detail&voteId="+voteId;

}

ids = buf.toString();

ids = ids.substring(0, ids.length() - 1);

。。。。。。。后续的操作就不加以描述了。

说明:上面代码中,如果确定是单选的话就更简单些,直接用String value = request.getParameter("key");就可以了;但如果不确定或者是多选的话就要用String[] values = reqest.getParameterValues("key"); 。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。