如果未在关联的单选按钮上选择任何值,则会发生下拉验证错误 [英] A dropdown validation error occurs if no value is selected on the associated radio button

查看:85
本文介绍了如果未在关联的单选按钮上选择任何值,则会发生下拉验证错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有两个单选按钮的表单:是"和否".如果选择是",则用户从两个下拉菜单中选择所需的内容.如果用户选择否",则表单的JavaScript将使两个下拉菜单均变灰.

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.

我的jQuery代码如下所示.我加了:

My jQuery code is shown below. I added:

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

但是没有用.

两个单选按钮的name属性均设置为discount.下拉菜单的id属性是#colour1#shade1.

Both radio buttons' name attribute is set to discount. The drop-down menus' id attributes are #colour1 and #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();

    });

推荐答案

尝试一下.

$( 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');
             }
        }

    });
});

这篇关于如果未在关联的单选按钮上选择任何值,则会发生下拉验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆