4

如果选择了单选按钮,如何禁用选择表单下拉菜单?

如果用户选择#no 单选按钮,我希望禁用#preference 表单,如果用户选择#yes 或#maybe,则再次启用它。

这是我的表单代码 -

<form name="xmas" id="xmas" action="send_mail.php" method="post">
            <input type="radio" name="choice" value="Yes" id="yes"/><label for="yes">Yes - I would like to take up the offer</label>
            <input type="radio" name="choice" value="No" id="no"/><label for="no">No - I'm not ready to order yet</label>
            <input type="radio" name="choice" value="Maybe" id="maybe"/><label for="maybe">Maybe - I'm not sure, would you please give me a call</label><br />
            <div class="secondlineform">
                <input type="text" name="name" class="textfield" value="Name" onfocus="if(this.value=this.defaultValue){this.value=''}; return false;"/>
                <input type="text" name="email" class="textfield" value="Email" onfocus="if(this.value=this.defaultValue){this.value=''}; return false;"/>
                <input type="text" name="company" class="textfield" value="Company" onfocus="if(this.value=this.defaultValue){this.value=''}; return false;"/>
                <select class="dropselect" name="preference" label="Preference" id="preference">
                    <option selected="selected" disabled="disabled" value="Preference">Preference</option>
                    <option value="Alcohol Package">Alcohol Package</option>
                    <option value="Gift Card Package">Gift Card Package</option>
                </select>
                <input type="submit" value="" name="submit" id="submit" class="submit"/>

            </div>
        </form>

这是我的脚本代码 -

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>

​​​

  var update_select = function () {
    if ($('#no').is(':checked')) {
    $('#preference').attr('disabled', "disabled");
}
else {
    $("#preference").removeAttr("disabled");
}

};

$(update_select);
$('#no').change(update_select);

</script>

非常感谢任何答案!我研究这个问题已经很久了,我发现的所有解决方案似乎都不能解决我的问题。

4

4 回答 4

2

您正在为No收音机添加更改事件,将其更改为:

$("input[name='choice']").change(update_select);

演示:: jsFiddle

于 2012-11-25T08:05:20.407 回答
2
$('input[type=radio]').change(update_select);​
于 2012-11-25T08:10:49.530 回答
0

您需要将该update_select功能应用于单选按钮的其余部分,如下所示

var update_select = function () {
$('#preference').attr('disabled', $('#no').is(':checked'));
};


$('#no').change(update_select);
$('#yes').change(update_select);
$('#maybe').change(update_select);

这是小提琴:http: //jsfiddle.net/QXkhM/

于 2012-11-25T08:06:06.443 回答
0

用 ul 包裹您的单选按钮列表并给它一个 ID。

例如:

        <ul id="offer">
        <input type="radio" name="choice"  value="Yes" id="yes"/><label for="yes">Yes - I would like to take up the offer</label>
        <input type="radio" name="choice"  value="No" id="no"/><label for="no">No - I'm not ready to order yet</label>
        <input type="radio" name="choice"  value="Maybe" id="maybe"/><label for="maybe">Maybe - I'm not sure, would you please give me a cal
        l</label><br />
        </ul>

var update_select = function () {
    if ($('#no').is(':checked')) {
        $('#preference').attr('disabled', "disabled");
    }
    else {
        $("#preference").removeAttr("disabled");
    }
};
$(update_select);
$('#offer').change(update_select);

它会起作用

于 2012-11-25T08:12:40.393 回答