[英]Un Select or Select check box using javascript
I have check boxes in the table created dynamically using JSF and i get the HTML output like the following, 我在使用JSF动态创建的表中有复选框,并且得到如下所示的HTML输出,
<input name="testForm:j_idt78:0:negative" id="testForm:j_idt78:0:negative" onchange='PrimeFaces.ab({s:this,e:"change",p:"testForm:j_idt78:0:negative"});' type="checkbox">
<input name="testForm:j_idt78:0:positive" id="testForm:j_idt78:0:positive" onchange='PrimeFaces.ab({s:this,e:"change",p:"testForm:j_idt78:0:positive"});' type="checkbox">
<input name="testForm:j_idt78:0:na" id="testForm:j_idt78:0:na" onchange='PrimeFaces.ab({s:this,e:"change",p:"testForm:j_idt78:0:na"});' type="checkbox">
if i select one check box then other two should be un selected and it is same for all the checkbox, at any time only one check box should be selected in a row. 如果我选中一个复选框,则应取消选中其他两个复选框,并且所有复选框都相同,在任何时候都应仅选中一个复选框。
the above is the sample of only one row and how to do do this dynamically for N
number rows[the table may contain any number of rows.] 以上是仅一行的示例,以及如何对
N
数字行动态执行此操作[该表可以包含任意数量的行。]
the following is the actual code which generates the check boxes dynamically 以下是实际生成复选框的实际代码
<ui:repeat var="item" value="#{testController.itemsList}" varStatus="loop">
<p:row>
<p:column>
<h:outputText value="#{item.desc}" />
</p:column>
<p:column>
<h:outputText value="#{item.code}" />
</p:column>
<p:column>
<h:selectBooleanCheckbox value="#{testController.negative}" id="negative"> </h:selectBooleanCheckbox>
</p:column>
<p:column>
<h:selectBooleanCheckbox value="#{testController.positive}" id="positive"> </h:selectBooleanCheckbox>
</p:column>
<p:column>
<h:selectBooleanCheckbox value="#{testController.na}" id="na"> </h:selectBooleanCheckbox>
</p:column>
</p:row>
</ui:repeat>
I tried something like the following but still it is not un selecting the check boxes in the row and gives me the following exception in the console 我尝试了以下操作,但仍然不是取消选中该行中的复选框,并在控制台中给了我以下异常
SCRIPT5022: Syntax error, unrecognized expression: unsupported pseudo: j_idt78
function selectUnSelect(elementName) {
var fields = elementName.split(':');
var formName = fields[0];
var jsfId = fields[1];
var fieldIndex = fields[2];
var propertyName = fields[3];
console.log(formName+":"+jsfId+":"+fieldIndex+":positive");
if(propertyName == 'positive'){
$("#"+formName+":"+jsfId+":"+fieldIndex+":positive").attr('checked', true);
$("#"+formName+":"+jsfId+":"+fieldIndex+":negative").attr('checked', false);
$("#"+formName+":"+jsfId+":"+fieldIndex+":na").attr('checked', false);
}
if(propertyName == 'negative'){
$("#"+formName+":"+jsfId+":"+fieldIndex+":positive").attr('checked', false);
$("#"+formName+":"+jsfId+":"+fieldIndex+":negative").attr('checked', true);
$("#"+formName+":"+jsfId+":"+fieldIndex+":na").attr('checked', false);
}
if(propertyName == 'na'){
$("#"+formName+":"+jsfId+":"+fieldIndex+":positive").attr('checked', false);
$("#"+formName+":"+jsfId+":"+fieldIndex+":negative").attr('checked', false);
$("#"+formName+":"+jsfId+":"+fieldIndex+":na").attr('checked', true);
}
}
Please help me to fix the issue. 请帮助我解决问题。
why don't you just use type="radio"
buttons, they have desired behavior natively. 您为什么不只使用
type="radio"
按钮,它们本来就具有所需的行为。 just make sure all radio buttons in the same row have same name
attribute 只要确保同一行中的所有单选按钮都具有相同的
name
属性
checkbox inputs have a property named checked which you can change to true or false to make them checked or unchecked respectively. 复选框输入具有名为checked的属性,您可以将其更改为true或false,以分别使它们处于选中状态或未选中状态。
document.getElementById('choice1').checked = true; // Check
document.getElementById('choice1').checked = false; // Uncheck
check out the example below. 请查看以下示例。 https://jsfiddle.net/1zty7mcL/
https://jsfiddle.net/1zty7mcL/
var checkboxes = [
document.getElementById('choice1'),
document.getElementById('choice2'),
document.getElementById('choice3'),
];
var uncheckAll = function () {
checkboxes.forEach(function (checkbox) {
checkbox.checked = false;
});
}
checkboxes.forEach(function (checkbox) {
checkbox.addEventListener('click', function () {
uncheckAll();
this.checked = true;
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.