简体   繁体   English

<JSP>获得复选框的价值

[英]<JSP> Getting value of check boxes

Im stuck on getting check box's value here. 我坚持在这里获取复选框的价值。 Following is my code. 以下是我的代码。

<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> 

So when I check the "A", its its value "normalChk" should be sent through frm. 所以当我检查“A”时,它的值“normalChk”应该通过frm发送。 Ans when I click on the CHECK button, its value should be displayed. 单击CHECK按钮时,应显示其值。 But for some reason, it doesn't work. 但由于某种原因,它不起作用。 Can anyone tell me why? 谁能告诉我为什么? and How to fix it? 以及如何解决它?

You can try this: 你可以试试这个:

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

.prop get the value of a property for the first element in the set of matched elements. .prop获取匹配元素集中第一个元素的属性值。

Try this: 尝试这个:

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

HTML: 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM