【问题标题】:Get RadioButton of List by Value按值获取列表的 RadioButton
【发布时间】:2012-10-23 18:37:21
【问题描述】:

我有一个包含数据和单选按钮的表格,可以检查:接受、待定、拒绝。 在保存时,我想检查是否有任何单选按钮仍处于待处理状态。 我该怎么做?

单选按钮如下所示:

<td class="fit">
 <table>
  <tr>
   <td class="pad0"><div class=""><fmt:message key='status.registration.registered'/>:</div></td>
   <c:if test="${participation.registrationStatus == 2}"><td class="pad0 padL10"><spring:radiobutton path="assignmentFormArray[${i}].registrationStatus" value="2" checked="checked"/></td></c:if>
   <c:if test="${participation.registrationStatus != 2}"><td class="pad0 padL10"><spring:radiobutton path="assignmentFormArray[${i}].registrationStatus" value="2"/></td></c:if>
  </tr><tr>
   <td class="pad0"><div class=""><fmt:message key='status.registration.pending'/>:</div></td>
   <c:if test="${participation.registrationStatus == 1}"><td class="pad0 padL10"><spring:radiobutton path="assignmentFormArray[${i}].registrationStatus" value="1" checked="checked"/></td></c:if>
   <c:if test="${participation.registrationStatus != 1}"><td class="pad0 padL10"><spring:radiobutton path="assignmentFormArray[${i}].registrationStatus" value="1"/></td></c:if>
  </tr><tr>
   <td class="pad0"><div class=""><fmt:message key='status.registration.declined'/>:</div></td>
   <c:if test="${participation.registrationStatus == 6}"><td class="pad0 padL10"><spring:radiobutton path="assignmentFormArray[${i}].registrationStatus" value="6" checked="checked"/></td></c:if>
   <c:if test="${participation.registrationStatus != 6}"><td class="pad0 padL10"<spring:radiobutton path="assignmentFormArray[${i}].registrationStatus" value="6"/></td> </c:if>
  </tr>
 </table>
</td>

我尝试了以下 Javascript 函数:

function save(){
 alert($("input:radio[value='1']:checked"));
}

不幸的是,这不起作用。即使没有选择值为 1 的单选按钮,它也会始终通过对象发出警报。

【问题讨论】:

    标签: javascript jquery jsp


    【解决方案1】:
       $('input:radio[name=name of radio button]:checked').val();
    

    这将为您提供所选单选按钮的值..您可以检查该按钮是挂起还是接受或拒绝...

    【讨论】:

    • 但我可能有 50 个单选按钮,我需要知道其中一个的值为 1(=待定)...使用您的代码,我可以找出,如果一组单选按钮有值 1(=待定)。
    【解决方案2】:

    我找到了解决方案。但感谢任何想提供帮助的人:

    function save() {
     var isPending = false;
     $("input[type='radio']:checked").each(function(){
        if ($(this).val() == 1){
            isPending = true;
        };
     });
     if (isPending){
        alert("isPending");
     }
    }
    

    【讨论】:

      【解决方案3】:

      我想你可以创建一个简单的布尔数组 它存储单选按钮的值 在更改单选按钮时,您每次都需要调用 set 方法 这就像一个 bean 来设置和获取表单元素的值,保存时 你可以简单地检查布尔数组

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-06
        • 2018-09-19
        • 1970-01-01
        • 2017-06-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        相关资源
        最近更新 更多