如果单选框是唯一显示的,则自动选中它

发布于 2024-09-05 23:24:05 字数 710 浏览 2 评论 0原文

因此,我有以下形式的单选框

<li><input type="radio" name="names" value="blah"><a>Some text (blah)</a></li>

There are 100 plus of such radio Buttons.

现在,我有一个 jQuery 过滤器,来自此处。过滤器工作良好。

但是,我想设置一个 jQuery 语句,如果单选按钮是显示的单选按钮中唯一剩下的,则该语句将自动选择该单选按钮。

该过滤器使用 show()hide(),我认为这意味着它将其从 display: block 切换到 display: none;

我自己做这件事的最佳方法看起来像这样,但它不起作用:

     if($('li').attr({"display":"block"}).size()==1)
      {
      $('input[cvs_name]:eq(1)').attr('checked', 'checked');
      }

我怎样才能做到这一点?

So, I have radio boxes of the form

<li><input type="radio" name="names" value="blah"><a>Some text (blah)</a></li>

There are 100 plus of these radio buttons.

Now, I have a jQuery filter, from here. The filter works well.

But, I want to set a jQuery statement that will auto select the radio button if it is, amongst the shown, the only one remaining.

The filter uses show() and hide(), which I presume means it switches it from display: block to display: none;

My best crack at doing this on my own looks like this, but it's not working:

     if($('li').attr({"display":"block"}).size()==1)
      {
      $('input[cvs_name]:eq(1)').attr('checked', 'checked');
      }

How can I do this?

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

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

发布评论

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

评论(2

风追烟花雨 2024-09-12 23:24:06
var $visible = $('ul').find('li:visible');
if($visible.length == 1) {
    $visible.find('input:radio').attr('checked','checked');
}
var $visible = $('ul').find('li:visible');
if($visible.length == 1) {
    $visible.find('input:radio').attr('checked','checked');
}
贱贱哒 2024-09-12 23:24:06

试试这个:

if ($("li:visible").length === 1) {
    $("li:visible input").attr("checked", "checked");
}

jQuery 有一个 visible 过滤器,它只选择元素哪些不是隐藏的。 (当然,还有一个 hidden 过滤器,用于选择那些哪些是。)

Try this:

if ($("li:visible").length === 1) {
    $("li:visible input").attr("checked", "checked");
}

jQuery has a visible filter which only selects elements which aren't hidden. (And, of course, a hidden filter which selects those which are.)

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