简体   繁体   English

如何使用 JavaScript 设置 p:selectBooleanCheckbox 的值

[英]How to set value of p:selectBooleanCheckbox with JavaScript

When the focus is on the checkbox and the user presses enter, I need the value to be active or inactive depending on which its state, and also focus a button, the focus works but the value is not being updated, this is what I have so far:当焦点位于复选框上并且用户按下 Enter 键时,我需要根据其状态使值处于活动状态或非活动状态,并且还需要聚焦按钮,焦点有效但值未更新,这就是我所拥有的迄今为止:

SelectBooleanCheckBox:选择布尔复选框:

<p:selectBooleanCheckbox id="sbcActivoProp" widgetVar="sbcActivoPropWV" onchange="document.getElementById('frmGuardarActualizarPropiedad:btnGuardarPropiedad').focus(); return false;"
    binding="#{programaAccesoMB.sbcActivoProp}"
    label="#{etiquetasMsg.general_activo}" disabled="false">
</p:selectBooleanCheckbox>

Javascript: Javascript:

<script type="text/javascript">
                                 var focusSbcActivoProp = document.getElementById('frmGuardarActualizarPropiedad:sbcActivoProp_input');           
                                    if (focusSbcActivoProp != null) {
                                     focusSbcActivoProp.onkeydown = function(event) {
                                     if (event.keyCode == 13) {
                                         var ischecked = sbcActivoPropWV.input.is(':checked');
                                         if(ischecked){
                                             console.log('is checked');
                                             document.getElementById('frmGuardarActualizarPropiedad:sbcActivoProp').value=false;
                                         }else{
                                             console.log('is not checked');
                                             document.getElementById('frmGuardarActualizarPropiedad:sbcActivoProp').value=true;
                                             }
                                         document.getElementById('frmGuardarActualizarPropiedad:btnGuardarPropiedad').focus();
                                     }
                                     return false;
                                     }
                                };
                            </script> 

The PrimeFaces documentation has the answer for you: PrimeFaces 文档为您提供了答案:

Client Side API客户端 API

Widget: PrimeFaces.widget.SelectBooleanCheckbox小部件: PrimeFaces.widget.SelectBooleanCheckbox

 Method Params ReturnType Description check() - void Checks the checkbox. uncheck() - void Unchecks the checkbox. toggle() - void Toggles check state.

I checked and this works at least for 7.0 and 8.0 (did not look for earlier versions but it is very simple to just try)我检查过,这至少适用于 7.0 和 8.0(没有寻找早期版本,但尝试非常简单)

Thanks to the documentation referenced from @Kukeltje, this is how the javascript code ended being:多亏了@Kukeltje 引用的文档,javascript 代码是这样结束的:

<script type="text/javascript">
                                 var focusSbcActivoProp = document.getElementById('frmGuardarActualizarPropiedad:sbcActivoProp_input');           
                                    if (focusSbcActivoProp != null) {
                                     focusSbcActivoProp.onkeydown = function(event) {
                                     if (event.keyCode == 13) {
                                        sbcActivoPropWV.toggle();
                                        document.getElementById('frmGuardarActualizarPropiedad:btnGuardarPropiedad').focus();
                                     }
                                     return false;
                                     }
                                };
                            </script>

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

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