Javascript 如果选中收音机,则禁用选择窗体

Javascript 如果选中收音机,则禁用选择窗体,javascript,jquery,forms,select,radio,Javascript,Jquery,Forms,Select,Radio,如果选择了单选按钮,如何禁用“选择表单”下拉列表 如果用户选择“否”单选按钮,我希望禁用“首选项”表单,如果用户选择“是”或“可能”,则再次启用该表单 这是我的表格代码- <form name="xmas" id="xmas" action="send_mail.php" method="post"> <input type="radio" name="choice" value="Yes" id="yes"/><label for="yes

如果选择了单选按钮,如何禁用“选择表单”下拉列表

如果用户选择“否”单选按钮,我希望禁用“首选项”表单,如果用户选择“是”或“可能”,则再次启用该表单

这是我的表格代码-

<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\u select=函数(){
如果($('#no')。是(':checked')){
$(“#首选项”).attr('disabled','disabled”);
}
否则{
$(“#首选项”).removeAttr(“禁用”);
}
};
$(更新_选择);
$('#否')。更改(更新选择);

非常感谢您的回答!我已经对此进行了多年的研究,我发现所有的解决方案似乎都不能解决我的问题。

如果您只为
收音机添加更改事件,请将其更改为:

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

演示:

您需要对单选按钮的其余部分应用
update\u select
功能,如下所示

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


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

这里是小提琴:

用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);
    是的,我想接受这个提议 不,我还没准备好点菜 也许-我不确定,你能给我打个电话吗 l
变量更新_选择=函数(){ 如果($('#no')。是(':checked')){ $(“#首选项”).attr('disabled','disabled”); } 否则{ $(“#首选项”).removeAttr(“禁用”); } }; $(更新_选择); $('offer')。更改(更新选择);

它会工作的

看不到错误在哪里。。。尝试使用
$(“#首选项”).prop('disabled',true)
,然后使用
('disabled',false)
抱歉,但是为什么需要执行此操作
$(更新\u选择)?更新选择是一个函数,对吗?哦,实际上有一些东西。。。为什么`
$(更新\u选择)
那应该做什么?你是说
update\u select()
?我在这个论坛上尝试了所有答案,但不幸的是,没有一个对我有用。我对剧本不太了解。。。我有什么遗漏吗?您是否在控制台中看到任何错误。。。?您在JSFIDLE..中看到演示链接了吗?控制台中没有错误,是的,我看到了演示链接。我的代码和你的代码完全一样,但对我来说似乎根本不起作用。我真的很感谢你的帮助。谢谢你这么快的回复,我会继续努力的。我一定少了点什么。也许我没有正确地引用jquery库。无论如何谢谢你!很抱歉有一个错误!SyntaxError:非法字符[Break On This Error]$(“input[name='choice']”)。更改(update_select);​
$('input[type=radio]').change(update_select);​
        <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);