【问题标题】:<JSP> Getting value of check boxes<JSP> 获取复选框的值
【发布时间】:2013-12-23 08:18:11
【问题描述】:

我坚持在这里获取复选框的值。以下是我的代码。

<script type="text/javascript">
    fnChkGrp = function() {
        alert($('#reqType').val()
    )}
</script>   

<form id="frm" name="frm" method="post" action="">
    <input type="hidden" id="reqType" name="reqType"/>
    <table class="tableBB mgT10" >
        <tr>
            <td>
                <span id="reqType" style="display:block;">
                    <span class="chk"><label><input type="checkbox" id="normal" name="normal" value="normalChk"/>A</label></span>
                    <span class="chk"><label><input type="checkbox" id="urgent" name="normal" value="urgentChk"/>B</label></span>
                </span>     
            </td>

            <div class="area_btnA clfix mgB20">
                <a href="#" onclick="fnChkGrp();return false;" class="btnA"><strong>CHECK</strong></a>
            </div> 

所以当我检查“A”时,它的值“normalChk”应该通过frm发送。 Ans 当我点击 CHECK 按钮时,它的值应该会显示出来。 但由于某种原因,它不起作用。谁能告诉我为什么?以及如何解决?

【问题讨论】:

    标签: javascript jquery html checkbox


    【解决方案1】:

    你可以试试这个:

    if($('input[name=normal]').prop(':checked').val()==true)
    {
        alert("Checked");
    }
    else
    {
        alert("Unchecked");
    }
    

    .prop 获取匹配元素集中第一个元素的属性值。

    【讨论】:

      【解决方案2】:

      试试这个:

      fnChkGrp = function() {
         var checkboxes = document.getElementsByName("normal");
          for (var i = 0; i < checkboxes.length; i++)
          {
              if (checkboxes[i].checked)
                  alert(checkboxes[i].value);
          }
      }
      

      HTML:

      <form id="frm" name="frm" method="post" action="">
      <input type="hidden" id="reqType" name="reqType"/>
      <table class="tableBB mgT10" >
      <tr>
      
          <td>
              <span id="reqType" style="display:block;">
                  <span class="chk"><label><input type="checkbox" id="normal" name="normal" value="normalChk"/>A</label></span>
                  <span class="chk"><label><input type="checkbox" id="urgent" name="normal" value="urgentChk"/>B</label></span>
              </span>     
          </td>
          </tr>
      </table>
          <div class="area_btnA clfix mgB20">
              <a href="#" onclick="fnChkGrp();return false;" class="btnA"><strong>CHECK</strong></a>
          </div>
      </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多