简体   繁体   English

如果在关联的单选按钮上未选择任何值,则会发生下拉验证错误

[英]A dropdown validation error occurs if no value is selected on the associated radio button

I have a form with two radio buttons: "Yes" and "No". 我有一个带有两个单选按钮的表单:“是”和“否”。 If "Yes" is selected, the user selects what they want from two drop-down menus. 如果选择“是”,则用户从两个下拉菜单中选择所需的内容。 If the user selects "No", the form's JavaScript will grey out both drop-down menus. 如果用户选择“否”,则表单的JavaScript将使两个下拉菜单均变灰。

My jQuery code is shown below. 我的jQuery代码如下所示。 I added: 我补充说:

if(!$("#discount").prop("disabled") && $("#colour1, #shade1").val() == 'please select'){
    //alert('Please select');
}

but it did not work. 但它没有用。

Both radio buttons' name attribute is set to discount . 两个单选按钮的name属性都设置为discount The drop-down menus' id attributes are #colour1 and #shade1 . 下拉菜单id属性#colour1#shade1

The first value of each drop down is "please select": 每个下拉菜单的第一个值是“请选择”:

 $( function(){
        $("input[name='discount']").click(function() {
            $("#colour1, #shade1")
            .prop("disabled", this.value == 'No');
            if(!$("#discount").prop("disabled") && $("#colour1, #shade1").val() == 'please select'){ //alert('Please select'); }. 
        }).click();

    });

Try this. 尝试这个。

$( function(){
    $("input[name='discount']").click(function() {
        var isDisabled = (this.value == 'No');
        $("#colour1, #shade1").prop("disabled", isDisabled);

        if(!isDisabled){
             //Please select option is selected
             if($("#colour1")[0].selectedIndex == 0){
                 alert('Please select color');
             }
             //Please select option is selected
             if($("#shade11")[0].selectedIndex == 0){
                 alert('Please select shade');
             }
        }

    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM