检查所有无线电按钮(JSF组件) jQuery



我有一个我要迭代数据表的表格,每行都有一组组件,其中一个是:

<h:selectOneRadio id="chargeWaive" onclick="alert(this.id);" >              <f:selectItem itemLabel="Charge" itemValue="charge" />                  <f:selectItem itemLabel="Waive" itemValue="waive" />
</h:selectOneRadio>

我添加了两个链接,这些链接触发了两个相似的功能:

<a href="#" onclick="selectAllCharge();">
   <h:outputText value="Charge All" />
</a>
<a href="#" onclick="selectAllWaive();">
   <h:outputText value="Waive All" />
</a>

因此,当用户单击这些链接时,应检查所有费用/放射性radiobuttons。

我尝试通过使用以下代码检查第一个广播按钮(测试目的),但是我总是会遇到相同的错误:

$('#frmResults:billingRecordId:0:chargeWaive:0').attr('checked', true);   $('#frmResults:billingRecordId:0:chargeWaive:0').attr('checked', 'checked');
$('#frmResults:billingRecordId:0:chargeWaive:0').prop("checked", true); 

我遇到的错误是: sintax错误,未识别的表达式:BillingRecordID

我确实知道ID正确

<input type="radio" name="frmResults:billingRecordId:0:chargeWaive" id="frmResults:billingRecordId:0:chargeWaive:0" value="charge" onclick="alert(this.id);" /><label for="frmResults:billingRecordId:0:chargeWaive:0"> Charge</label>
<input type="radio" name="frmResults:billingRecordId:0:chargeWaive" id="frmResults:billingRecordId:0:chargeWaive:1" value="waive" onclick="alert(this.id);" /><label for="frmResults:billingRecordId:0:chargeWaive:1"> Waive</label>

所以在这一点上,我不知道这里缺少什么。有什么想法吗?

jQuery使用CSS选择器在HTML DOM树中选择元素。

:是代表结构伪类开始的CSS选择器中的特殊字符。因此,如果您使用

$('#frmResults:billingRecordId')

然后,它基本上是在寻找具有frmResults ID的HTML元素,并且具有匹配billingRecordId的伪类。但是,由于billingRecordId根本不是一个有效的伪类类,因此什么也找不到。

您基本上需要逃脱CSS选择器语法中的结肠。

$('#frmResults\:billingRecordId\:0\:chargeWaive\:0')

或IMO清洁器,使用[id]属性选择器。

$('[id="frmResults:billingRecordId:0:chargeWaive:0"]')

或者,要摆脱链式ID和索引。

$('[id$=":chargeWaive"]:first :radio:first')

(; quot;&quot'选择 :chargeWaive上的ID结束的元素,获取第一个,然后从中获取第一个广播按钮&quot;)

另请参见:

  • 如何使用jQuery选择JSF组件?

作为一种完全不同的替代方法,您还可以通过在Backing Bean的Ajax侦听器方法中相应地设置模型值来执行JSF AJAX调用以预选所需的radiobuttons。

结肠可能会在jQuery选择器中引起问题。尝试用双重斜向ala逃脱它们:

$('#frmResults\:billingRecordId\:0\:chargeWaive\:0').attr('checked', true);

您是否尝试过这种方式

也使用.prop()代替.attr()

$('[id^="frmResults:billingRecordId:0:chargeWaive:"]').prop("checked", true); 

最新更新