使用 jQuery 检查某些单选按钮是否被选中并应用 CSS 块样式
我正在尝试检查是否选择了以下单选按钮之一,并且开始认为必须有更好的方法使用不同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 属性以选择器开头
和检查是否为0。
如果你可以给这些复选框一个类名,那么你可以使用类似的名称
You can use Attribute Starts With Selector
and check whether it is 0 or not.
If you can give these checkbooxes a class name then you can use something like
您可能会选择
.is()
例如
您的解决方案可能
是您需要将
:checked
更改为:selected
此外:如果您使用 ids,则应该使用
#
-选择器,因为:ids 必须是唯一的!you might go for
.is()
eg
your solution might be
maybe you will need to change
:checked
to:selected
furthermore: you should go for
#
-selectors if you use ids, due: ids must be unique!