Javascript jQuery选择器中的括号

Javascript jQuery选择器中的括号,javascript,jquery,textbox,radio-button,show-hide,Javascript,Jquery,Textbox,Radio Button,Show Hide,我的jquery遇到了一些问题,我真的无法让它工作 我正在做一个调查,每个问题的最后一个单选选项是other,因此用户可以输入自定义文本(如果愿意)。 我希望文本框被隐藏,直到或如果收音机被选中,文本框应该是对等的 我试过这样做: $(document).ready(function () { $("#custom_form_custom_field_2_block").hide(); $("input:radio[name=custom_form[custom_fie

我的jquery遇到了一些问题,我真的无法让它工作

我正在做一个调查,每个问题的最后一个单选选项是other,因此用户可以输入自定义文本(如果愿意)。 我希望文本框被隐藏,直到或如果收音机被选中,文本框应该是对等的

我试过这样做:

    $(document).ready(function () {
    $("#custom_form_custom_field_2_block").hide();

    $("input:radio[name=custom_form[custom_field_1]]").change(function () {
        if (this.value == "jeg har et helt 4. forslag") {
            $("#custom_form_custom_field_2_block").fadeIn();
        } else {
            $("#custom_form_custom_field_2_block").fadeOut();
        }

    });

    $("#custom_form_custom_field_2_block").focusout(function () {
        $("input:radio[name=Title]:checked").attr('value', $("#custom_form_custom_field_2_block").val());
    });
});
我在这里有一个到提琴山的链接:

希望你能帮忙, 当做
托马斯

我认为这句话就是问题所在:

$("input:radio[name=custom_form[custom_field_1]]")
括号(“[”和“]”)是css选择器中的保留字符(您正在将这样的选择器传递给jQuery的$()函数),并且使用括号作为元素名称的一部分。请尝试以下方法:

$("input:radio[name=custom_form\\[custom_field_1\\]]")
这将产生一个css选择器

input:radio[name=custom_form\[custom_field_1\]]
从而摆脱了括号。可能不适用于所有浏览器

编辑:以下是有关css选择器中转义字符的一些附加信息:

您必须在JSFIDLE中启用jQuery。使用左侧的菜单栏选择右侧版本。然后在Chrome/IE或Firefox中运行开发者工具栏。错误是
Uncaught错误:语法错误,无法识别的表达式:input:radio[name=custom\u form[custom\u field\u 1]