`
huanglz19871030
  • 浏览: 241925 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

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

阅读更多

A 获取下拉框的value值

1.1  js获取下拉框中的value值从而给出提示:
       jsp:<select name="faTypeSel" id="faTypeSel" style="position:relative">
                             <option value="">请选择收藏夹类型</option>
                          <c:forEach items="${faList}" var="fa">
                            <option value="${fa.type}">${fa.faDesc}</option>
                          </c:forEach>
                     </select>
      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中:<from ....><input type="<%if(WebConstants.VOTE_CHECK_TYPE_RADIO.equals(voteDetailInfo.getIs_multi())){%>radio<%}else{%>checkbox<%} %>" value="<%=vq.getId()%>" name="vote" /><%} %>

 <input type="button(submit)" id="joinVote" name="joinVote" value="立即投票" onclick="joinVote()"/></form>

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

2.1 对应的js:

     function joinVote()
   {

       var targetId = '<%=voteDetailInfo.getId()%>';

       var type = '<%=voteDetailInfo.getIs_multi()%>';
       var max= '<%=voteDetailInfo.getChoice_max()%>';
       var count = 0;
       var ids="";
         var items = document.getElementsByName("vote");
        for(var i=0;i<items.length;i++)
     {
      if(items[i].checked)
      {
          count++;
       ids+=items[i].value+",";
      }
     }
     if(<%=WebConstants.VOTE_CHECK_TYPE_CHECKBOX%> == type && count > max)
     {
      alert("提示:不能超过最多投票项数");
      return;
     }   
     if(ids=="")
     {
      alert("提示:嗨,您还没有选择投票项");
      return;
     }else
     {
      ids=ids.substring(0,ids.length-1);
         window.location.href='<%=request.getContextPath()%>/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<vote.length;i++)
           {
                 count++;
           buf.append(vote[i].toString()+",");
           }
           if(WebConstants.VOTE_CHECK_TYPE_CHECKBOX == Is_multi && count > Choice_max)
           {
            tipText = " 不能超过最多投票项数!";
               url = "/action?voteAction=0&amp;action=detail&amp;voteId="+voteId;
           }
          }
          else
          {
           tipText = " 您还没有选择投票项!";
           url = "/action?voteAction=0&amp;action=detail&amp;voteId="+voteId;
          }
      
       ids = buf.toString();
      ids = ids.substring(0, ids.length() - 1);

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

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

1
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics