【问题标题】:Why do php form arrays behave strangely/(unexpectedly) in jQuery为什么 php 表单数组在 jQuery 中表现异常/(意外)
【发布时间】:2011-10-08 17:35:57
【问题描述】:

为什么 jQuery 中 php 表单输入数组的行为很有趣?

有什么办法可以克服吗?

嗨,我写了这段代码,它可以工作.. 更新 21 个单选输入的形式,以强调选中单选旁边的标签。

*视觉上 - 不像在 html 标签中

$(function()
{       
for(count = 0;count<21;count++)
{
    result = $("input:radio[name=choice"+count+"]:checked").val();
    $("input[name=\"choice"+count+"\"][ value=\""+ result +"\"]").attr("class", "magic");
    $("div.radio:has(input.magic)").attr("class", "radio spell");   
}
$("input").click(function()
{
    $("div.radio:has(input.magic)").attr("class", "radio");
    $("input.magic").removeAttr("class", "magic");

    var count = 0;
    var result = 0;
    for(count = 0;count<21;count++)
    {
        result = $("input:radio[name=choice"+count+"]:checked").val();
        $("input[name=\"choice"+count+"\"][ value=\""+ result +"\"]").attr("class", "magic");
        $("div.radio:has(input.magic)").attr("class", "radio spell");   
    }
});
});

这是css

input[type="radio"] {
height: 20px;
}

input[type="radio"] + label {
color: #777;
font-weight:100;
letter-spacing: 1px;
}

input[type="radio"].magic + label {
color: black;
font-style: italic;
/*text-decoration:underline;*/
font-weight: 600;
letter-spacing: 0px;
text-align: center;
display:inline-block;
width: 80px;
}

div.radio.spell {
border: outset white 2px;
}

现在使用的无线电名称是choice0 -choice20 它工作正常。

在此之前我使用了选择[0] - 选择[20] 它根本不起作用。 它的行为很奇怪。

jQuery 代码类似于: 结果 = $("input:radio[name=choice["+count+"]]:checked").val(); ...

我想知道,有什么办法吗?

感谢您提供宝贵意见。

【问题讨论】:

  • 工作得很好,自己看看jsfiddle.net/MqQh4
  • 做这个选择[20] 你又创建了一个数组,你有没有相应地改变你的 ajax,所以它可以处理这个?

标签: php jquery html arrays forms


【解决方案1】:

[] 用于 jQuery 中的属性选择器,您需要对它们进行转义才能将它们用作字符串的一部分:$(":radio[name=choice\["+count+"\]]")

也就是说,这是一种操作元素组的相当不舒服的方式。简单的方法是创建一个反映这些组的 HTML 结构,并使用它来查找组的其他成员:

$(function() {
    $(":radio[name^=choice]:checked").each(function() {
        $(this).closest('div.radio').attr("class", "radio spell")
            .find("input[name^=choice]").attr("class", "magic");
    });
});

等等

【讨论】:

    猜你喜欢
    • 2022-01-16
    • 1970-01-01
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 2021-09-28
    相关资源
    最近更新 更多