执行代码,在提交按钮上执行验证单击 - jquery

时间:2012-02-05 23:08:14

标签: jquery

我有一个提交按钮,可以将信息发送给MySQL。如果在单选按钮为“是”时单击提交按钮,并且下拉值为“请选择”,我希望弹出验证错误。当选择“是”单选按钮时,它会弹出。

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

    });
});

1 个答案:

答案 0 :(得分:0)

试试这个。

$( function(){
    function validate(){
       var enabled = ($("input[name='discount']:checked").val() == 'Yes'); 
       if(enabled){
             //Please select option is selected
             if($("#colour1")[0].selectedIndex == 0){
                 alert('Please select color');
                 return false;
             }
             //Please select option is selected
             if($("#shade11")[0].selectedIndex == 0){
                 alert('Please select shade');
                 return false;
             }

        }
        return true;
    }

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

    $("input:submit").click(function(){
         return validate();
    });
});