Uncheck the box if specific values ​​are selected from the dropdown

I need to uncheck a checkbox when I select a specific value from the dropdown or when the user does not select any value from the dropdown. I am using JQuery at the moment. This is the code I'm using right now, but it won't work.

Script

<script src="js/jquery-1.8.3.js"></script>
<script>
function sdp4(str) {
       if (  
   $('#drop1').val()!= '' 
|| $('#drop1').val()== "In Process"
|| $('#drop1').val()== "KIV" )

{$('#checkbox1').prop('checked', false);} 
else 
{$('#checkbox1').prop('checked', true);}
</script>

      

Html

<select class="form-dropdown"  id="drop1" name="drop1" onchange="sdp4">
        <option value=""> </option>
        <option value="In Process"> In Process </option>
        <option value="KIV"> KIV </option>
        <option value="Completed"> Completed </option>
        </select>  
<input type="checkbox" checked="checked" id="checkbox1" name="checkbox1" />

      

+3


source to share


3 answers


Assuming the only time you want to check the checkbox will be selected when executed:

 $("#drop1").on('change', function () {
    var val = $(this).val();
    if (val === " " || val === "In Process" || val === "KIV") {
        $('#checkbox1').prop('checked', false);
        return;
    }
    $('#checkbox1').prop('checked', true);
});

      

and html:



<select class="form-dropdown" id="drop1" name="drop1">
    <option value=" "></option>
    <option value="In Process">In Process</option>
    <option value="KIV">KIV</option>
    <option value="Completed">Completed</option>
</select>
<input type="checkbox"  id="checkbox1" name="checkbox1"/>

      

Here is the FIDDLE

+2


source


You need to actually bind the call to sdp4

:

$("#drop1").on('change', sdp4);

      



At this point it would be overkill to use #drop1

in selectors when you might be using this

.

0


source


Try this: http://jsfiddle.net/7cqDB/

function sdp4(str) {
  if (str == '' || str == "In Process" || str == "KIV") {
      $('#checkbox1').prop('checked', false);
  } else {
      $('#checkbox1').prop('checked', true);
  }
}

$(function () {
   $('select').on('change', function () {
       var str = $(this).val();
       sdp4(str);
   });
});

      

0


source







All Articles