获取 JS 函数的 p:selectOneRadio 值

编程入门 行业动态 更新时间:2024-10-26 08:32:18
本文介绍了获取 JS 函数的 p:selectOneRadio 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 PF 的 selectOneRadio 来选择要下载的文件类型.我还有一个 commandButton 来使用 onclick 属性调用下载 servlet.问题是当我选择文件类型并单击按钮时,选择的值当然还没有提交.当我点击下载按钮时,我正在寻找某种方法来获得可用的所选值.

I have a PF's selectOneRadio to choose a file type to download. Also I have a commandButton to call a download servlet using onclick attribute. The problem is that when I choose file type and click the button, the chosen value is of course not yet submitted. I'm looking for some way to get the chosen value available when I click on a download button.

这是我的代码:

<p:selectOneRadio id="sorType" value="#{bean.type}" layout="custom"> <f:selectItem itemLabel="XML" itemValue="XML" /> <f:selectItem itemLabel="XLS" itemValue="XLS" /> <f:selectItem itemLabel="CSV" itemValue="CSV" /> </p:selectOneRadio> <p:commandButton type="button" ajax="false" onclick="return downloadFile('#{bean.type}');" />

推荐答案

如果你想在客户端检查选定的值,你需要为你的 p 定义 widgetVar 属性:selectOneRadio,例如:

If you want to check the selected value on client side, you will need to define widgetVar attribute for your p:selectOneRadio, for example:

<p:selectOneRadio widgetVar="widgetSorType" id="sorType" value="#{bean.type}" layout="custom"> <f:selectItem itemLabel="XML" itemValue="XML" /> <f:selectItem itemLabel="XLS" itemValue="XLS" /> <f:selectItem itemLabel="CSV" itemValue="CSV" /> </p:selectOneRadio>

这将允许轻松找到元素 - 然后您可以进一步使用它来检查实际选择了哪个值.我可以看到两个选项:

This will allow the element to be easily found - you can then use it further to check which value was actually selected. I can see two options how to do it:

function getSelectedTypeVer1() { return PF('widgetSorType').getJQ().find(':checked').val() || ""; } function getSelectedTypeVer2() { var inputs = PF('widgetSorType').inputs; for (var i = 0; i < inputs.length; i++) { if (inputs[i].checked) { return inputs[i].value; } } return ""; }

选择更适合您的方法 - 两者都将返回所选值或空字符串,以防未选择任何内容.所以剩下的就是在你按钮的 onclick 中调用它,例如:

Select whichever approach suits you better - both will return either the selected value or an empty string in case nothing has been selected. So all that remains is calling it in you button's onclick , so for example:

<p:commandButton type="button" ajax="false" onclick="return downloadFile(getSelectedTypeVer1());" />

在 PrimeFaces 5.2 上测试

Tested on PrimeFaces 5.2

更多推荐

获取 JS 函数的 p:selectOneRadio 值

本文发布于:2023-07-26 09:31:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1215512.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   JS   selectOneRadio

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!