使用 jQuery 检查某些单选按钮是否被选中并应用 CSS 块样式

发布于 2024-08-21 19:54:39 字数 1787 浏览 4 评论 0原文

我正在尝试检查是否选择了以下单选按钮之一,并且开始认为必须有更好的方法使用不同的 jQuery 方法来实现相同的结果。

例如:

if($("input[id='incRating_yes']:checked").val()) $('.question4').css('display','block');
if($("input[id='incRating_fro']:checked").val()) $('.question4').css('display','block');
if($("input[id='incRating_both']:checked").val()) $('.question4').css('display','block');

有什么想法吗?

其他详细信息:-

HTML:

<span class="clearfix">
<label>Do you want to allow users to add Interim Reviews?</label>
Yes <input type="radio" name="interimRev" id="interimRev_yes" value="y" <?php if($interimRev=='y') echo "checked";?>/>
No <input type="radio" name="interimRev" id="interimRev_no" value="n" <?php if($interimRev=='n') echo "checked";?>/>
</span>

<span class="question3" id="question3" style="display:none;">
<label>Include Rating?</label>
<p id="lbl_incRating_no"><input type="radio" name="incRating" id="incRating_no" value="n" <?php if($incRating=='n') echo "checked";?>/> No</p>
<p id="lbl_incRating_yes"><input type="radio" name="incRating" id="incRating_yes" value="y" <?php if($incRating=='y') echo "checked";?>/> Yes</p>
<p id="lbl_incRating_fro"><input type="radio" name="incRating" id="incRating_fro" value="fro" <?php if($incRating=='fro') echo "checked";?>/> Final Review Only</p>
<p id="lbl_incRating_both"><input type="radio" name="incRating" id="incRating_both" value="b" <?php if($incRating=='b') echo "checked";?>/> Both Interim and Final Reviews</p>
</span>

根据从“是否允许用户添加临时评论?”中选择的单选按钮,将显示某些单选按钮。

Php 验证提交后,我需要能够再次显示 UI 并确定检查哪些单选按钮,因此最初发布了 jQuery 代码。

I'm trying to check if one of the following radio buttons is selected and am beginning to think that there must be a better way of achieving the same result using a different jQuery approach.

E.g:

if($("input[id='incRating_yes']:checked").val()) $('.question4').css('display','block');
if($("input[id='incRating_fro']:checked").val()) $('.question4').css('display','block');
if($("input[id='incRating_both']:checked").val()) $('.question4').css('display','block');

Any ideas?

Additional details:-

HTML:

<span class="clearfix">
<label>Do you want to allow users to add Interim Reviews?</label>
Yes <input type="radio" name="interimRev" id="interimRev_yes" value="y" <?php if($interimRev=='y') echo "checked";?>/>
No <input type="radio" name="interimRev" id="interimRev_no" value="n" <?php if($interimRev=='n') echo "checked";?>/>
</span>

<span class="question3" id="question3" style="display:none;">
<label>Include Rating?</label>
<p id="lbl_incRating_no"><input type="radio" name="incRating" id="incRating_no" value="n" <?php if($incRating=='n') echo "checked";?>/> No</p>
<p id="lbl_incRating_yes"><input type="radio" name="incRating" id="incRating_yes" value="y" <?php if($incRating=='y') echo "checked";?>/> Yes</p>
<p id="lbl_incRating_fro"><input type="radio" name="incRating" id="incRating_fro" value="fro" <?php if($incRating=='fro') echo "checked";?>/> Final Review Only</p>
<p id="lbl_incRating_both"><input type="radio" name="incRating" id="incRating_both" value="b" <?php if($incRating=='b') echo "checked";?>/> Both Interim and Final Reviews</p>
</span>

Depending on the radio selected from "Do you want to allow users to add Interim Reviews?", certain radio buttons will be displayed.

After Php validates the submission I need to be able to show the UI again and determine which radio buttons are checked, hence the jQuery code originally posted.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末染殇 2024-08-28 19:54:39

您可以使用 属性以选择器开头

$("input[id^='incRating_']:checked").length

和检查是否为0。

如果你可以给这些复选框一个类名,那么你可以使用类似的名称

$("input:radio.yourclassname:checked").length

You can use Attribute Starts With Selector

$("input[id^='incRating_']:checked").length

and check whether it is 0 or not.

If you can give these checkbooxes a class name then you can use something like

$("input:radio.yourclassname:checked").length
娇俏 2024-08-28 19:54:39

您可能会选择 .is()

例如

var element = $('selector');
if (element.is(':checked')) {
   element.css('display', 'block');
}

您的解决方案可能

if ($('input#incRating_yes').is(':checked')
    || $('input#incRating_fro').is(':checked')
    || $('input#incRating_both').is(':checked')) {
    $('.question4').css('display', 'block');
}

是您需要将 :checked 更改为 :selected
此外:如果您使用 ids,则应该使用 #-选择器,因为:ids 必须是唯一的!

you might go for .is()

eg

var element = $('selector');
if (element.is(':checked')) {
   element.css('display', 'block');
}

your solution might be

if ($('input#incRating_yes').is(':checked')
    || $('input#incRating_fro').is(':checked')
    || $('input#incRating_both').is(':checked')) {
    $('.question4').css('display', 'block');
}

maybe you will need to change :checked to :selected
furthermore: you should go for #-selectors if you use ids, due: ids must be unique!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文