【问题标题】:SelectOneRadio set checked on radioButton在 radioButton 上选中 SelectOneRadio 集
【发布时间】:2016-10-24 22:16:29
【问题描述】:

我想使用 javascript 在 p:selectOneRadio 上设置检查状态。

我设法做到了,但它只适用于 selectOneRadio 列表中的第一个元素,这是代码。

<h:form id="formID">
    <p:outputPanel id="customPanel" style="margin-bottom:10px">
        <p:selectOneRadio id="customRadio" value="#{radioView.color}" layout="custom">
            <f:selectItem itemLabel="Red" itemValue="Red" />
            <f:selectItem itemLabel="Green" itemValue="Green" />
            <f:selectItem itemLabel="Blue" itemValue="Blue" />
        </p:selectOneRadio>

        <h:panelGrid columns="2" cellpadding="5" border="1">
            <p:radioButton id="opt1" for="customRadio" itemIndex="0" />
            <h:outputLabel for="opt1" value="Red" />

            <p:radioButton id="opt2" for="customRadio" itemIndex="1" />
            <h:outputLabel for="opt2" value="Green" />

            <p:radioButton id="opt3" for="customRadio" itemIndex="2" />
            <h:outputLabel for="opt3" value="Blue" />

            <p:commandButton value="select" ajax="true" onclick="selectRadios()" update="@form"/>
        </h:panelGrid>
    </p:outputPanel>
</h:form>

JavaScript

<script type="text/javascript">
    function selectRadios() {
        jQuery("input[name='formID:customRadio']:first").prop('checked', true);
    }
    ;
</script>

当我加载页面并单击命令按钮时,它检查了第一个单选按钮并且很好,但我也想知道如何检查其他两个。

另外,如果有其他方法可以在不使用 javascript 的情况下将状态更改为选中该单选按钮,请告诉我。

感谢您的帮助。

-凯瓦

【问题讨论】:

  • 您使用哪种 HTML 预处理器?你能发布生成的 HTML 吗?
  • 您是否要选择所有单选框?如果是,您使用的 SelectOneRadio 只能选择列表中的一项。
  • @MarkVincentOsea 我不想全选。我想要的只是在单击命令按钮时选择一个单选按钮的代码。如您所见,我设法做到了,但仅针对第一个单选按钮(三个中的第一个),我该如何为第二个或第三个单选按钮做到这一点
  • @LouysPatriceBessette HTML 预处理器是什么意思?
  • 我的意思是这个 HTML 编码语法 &lt;[tagname]:[type ? ] [attributes]&gt;... 它不是纯 HTML。 “预处理器”用于解释它。我不知道哪个...有很多。因此,如果您可以发布“真实”的 HTML,那将会有所帮助。

标签: java jquery jsf primefaces


【解决方案1】:

我再次努力...查看...您的 HTML。

«Primefaces 包含的编辑器» 的 HTML 结果肯定接近我现在在 sn-p 下面发布的内容。 (很简单,在语法障碍之后)

所以看看 jQuery 代码...
它应该接近您的主要关注点。

您的问题#1: «当我加载页面并单击命令按钮时,它检查了第一个单选按钮并且很好,但我也想知道如何检查其他两个»

→ 单选按钮的性质在组内是独占的。
如果要检查多个复选框,请查找复选框。

您的问题#2: «另外,如果有其他方法可以在不使用 javascript 的情况下将状态更改为选中该单选按钮,请告诉我。» p>

→ 是的...正在加载。
通过HTML checked attribute

其他
→ 没有。



奖金
我在 selectradios 的任何状态更改(由用户进行)时添加了 console.log。

var radioCollection = $("input[name='myRadio']");

var radioCheckedVal = 0;  // 0 is first element in zero-based collection.
                          // Change to make another radio selected onload.

// 1st radio input is selected onload
$(radioCollection).first().prop('checked', true);

$(radioCollection).click(function(){
    if( $(this).is("input:checked") ){
        radioCheckedVal = $(this).val();
        console.log( "Radio input checked: "+radioCheckedVal );
    }
});

$("#customPanel").change(function(){
    selectVal = $(this).val();
    console.log( "Dropdown selected: "+selectVal );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="formID">
  <select id="customPanel" style="margin-bottom:10px"><!-- p:outputPanel ?? -->
    <option value="Red">Red</option>
    <option value="Green">Green</option>
    <option value="Blue">Blue</option>
  </select>
  <table>
    <tr>
      <td>
        <input type="radio" name="myRadio" id="opt1" value="Red">
      </td>
      <td>
        <label for="opt1">Red</label>
      </td>
    </tr>
    <tr>
      <td>
        <input type="radio" name="myRadio" id="opt2" value="Green">
      </td>
      <td>
        <label for="opt1">Green</labe2>
      </td>
    </tr>
    <tr>
      <td>
        <input type="radio" name="myRadio" id="opt3" value="Blue">
      </td>
      <td>
        <label for="opt1">Blue</labe3>
      </td>
    </tr>
  </table>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2014-02-28
    • 1970-01-01
    • 2023-03-15
    • 2018-09-13
    • 2011-03-23
    相关资源
    最近更新 更多