0

In this part of code we need to disable submit button until all fields are selected or-else every thing gets messed up! So is it possible to disable the submit button ? If so kindly help us!!

           out.println("<html><body><form action=''>");
            out.println("<br><center><h1><font color='red'>Detailed DocID Status</font></h1>");
            out.println("<br> Year <select name='year'>");
            out.println("<option value=''>Select Year</option>");

            for(int i=2010;i<=2020;i++){
                out.println("<option value="+i+">"+i+"</option>");
            }
                out.println("</select>");

            out.println(" &nbsp;&nbsp;&nbsp; Month <select name='month'>");

                out.println("<option value=''>Select Month</option>");
                out.println("<option value='01'>Jan</option>");
                out.println("<option value='02'>Feb</option>");
                out.println("<option value='03'>Mar</option>");
                out.println("<option value='04'>Apr</option>");
                out.println("<option value='05'>May</option>");
                out.println("<option value='06'>June</option>");
                out.println("<option value='07'>July</option>");
                out.println("<option value='08'>Aug</option>");
                out.println("<option value='09'>Sep</option>");
                out.println("<option value='10'>Oct</option>");
                out.println("<option value='11'>Nov</option>");
                out.println("<option value='12'>Dec</option>");

                out.println("</select>");

            out.println(" &nbsp;&nbsp;&nbsp; Status : <select name='status'><option value='Select Status'>Select Status</option><option value='Approved'>Approved</option><option value='Clarifications'>Clarifications</option><option value='Pending'>Pending</option></select> &nbsp;&nbsp;&nbsp; ");
            out.println("<input type='submit'id='submit' name='subBtn' value='Submit' disable='disabled' onClick=doenable()>");
            out.println("</form></body></html>");
            out.println("<br><br>");

Function we tried

 function doenable()
        {
            if(document.getElementById("year")!=null)
            {
                if(document.getElementById("month")!=null)
                    {
                        if(document.getElementById("status")!=null)
                            {
                                document.getElementById("subBtn").disabled=false;
                            }
                    }

              }

        }
4

2 回答 2

1
$("select").change(function() {
    if ($("select").filter(function () {
       return $(this).value == "";
    }).length == 0) {
     jQuery('#submit').removeAttr('disabled');
} else {
     jQuery('#submit').attr('disabled','disabled');
}

In your code, you're making two mistakes:

  1. You don't use .value to tell whether the field is selected.
  2. The handler should be in the onchange handler of the <select> elements, not the click handler of the submit button.
于 2013-05-15T17:14:56.120 回答
0

Using jQuery attach a function on select's change event

jQuery('select').change(function(){
    valid = true;
    jQuery('select').each(function(){
        if (jQuery(this).val() == ''){
            valid = false;
            return false;
        }
    });

    if (valid == true){
        jQuery('#submit').removeAttr('disabled');
    } else {
        jQuery('#submit').attr('disabled','disabled');
    }    
});
于 2013-05-15T17:07:42.353 回答