检查是否在 jQuery 中选择单选按钮时出现奇怪的行为

发布于 2024-08-30 07:32:11 字数 1206 浏览 2 评论 0原文

我在我的 jQuery 中进行了以下检查,我认为它工作正常,可以查看单选按钮是否被选中。

if ($("input[@name='companyType']:checked").attr('id') == "primary") {
   ...
}

这是单选按钮:

    <p>
        <label>Company Type:</label>
        <label for="primary"><input onclick="javascript: $('#sec').hide('slow');$('#primary_company').find('option:first').attr('selected','selected');" type="radio" name="companyType" id="primary" checked />Primary</label>
        <label for="secondary"><input onclick="javascript: $('#sec').show('slow');" type="radio" name="companyType" id="secondary" />Subsidiary</label>              
    </p>

然后,它突然停止工作(或者我是这么认为的)。我做了一些调试,最后意识到它返回的是“approved_status”的 id。在我的表单的其他地方,我有一个名为“approved_status”的复选框。我意识到,当我最初测试这个时,我必须在approved_status为假的记录上进行测试。而且,现在我的大多数 Approved_statuses 都是 true/checked。

我将代码更改为:

var id = $("input:radio[@name='companyType']:checked").attr('id');
alert(id);
if (id == "primary") {

现在它可以正确返回“primary”或“secondary”作为 id。

所以,它正在工作,但似乎它根本没有检查名称,现在只是检查单选按钮。我只是想知道为了将来的使用,原始代码有什么问题,b/c 我可以看到一个页面上可能有 2 个不同的无线电集,然后我的新修复程序可能无法工作。谢谢!

I had the following check in my jQuery which I thought was working fine to see if a radio button was checked.

if ($("input[@name='companyType']:checked").attr('id') == "primary") {
   ...
}

Here's the radiobuttons:

    <p>
        <label>Company Type:</label>
        <label for="primary"><input onclick="javascript: $('#sec').hide('slow');$('#primary_company').find('option:first').attr('selected','selected');" type="radio" name="companyType" id="primary" checked />Primary</label>
        <label for="secondary"><input onclick="javascript: $('#sec').show('slow');" type="radio" name="companyType" id="secondary" />Subsidiary</label>              
    </p>

Then, it suddenly stopped working (or so I thought). I did some debugging and finally realized that it was returning an id of "approved_status". Elsewhere on my form I have a checkbox called "approved_status". I realized that when I originally tested this, I must have testing it on records where approved_status is false. And, now most of my approved_statuses are true/checked.

I changed the code to this:

var id = $("input:radio[@name='companyType']:checked").attr('id');
alert(id);
if (id == "primary") {

And it's now properly returning "primary" or "secondary" as the id.

So, it is working, but it seems that it's not checking the name at all and now just checking radio buttons. I just want to know for future use, what's wrong with the original code b/c I can see possibly having 2 different radio sets on a page and then my new fix probably wouldn't work. Thanks!

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

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

发布评论

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

评论(1

乖乖兔^ω^ 2024-09-06 07:32:11

试试这个:

var id = $("input[name='companyType']:checked").attr('id');
alert(id);
if (id == "primary") {

Try this:

var id = $("input[name='companyType']:checked").attr('id');
alert(id);
if (id == "primary") {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文