0

I have a select box with 30 values, one of them is 'selected' by default. What I need to do is if anything but the 'selected' value is selected change a variable to true.

     $('.secondNext').click(function(){
      var vali_field = $(".udf-selection option:selected").val(),
      id = $('.hiddenRuleID').val(),
      tranType = [],         
      ruleStep = 'step2',
      tranTypeSel = false,
      validFieldSel = false;

      $('.checkboxGroup input[type="checkbox"]').each(function() {
          if ($(this).is(":checked")) {
              tranTypeSel = true;

          }
      });
        if (tranTypeSel == false) {
          $('.secondNext').removeClass('next');
          $('.tranTypeError').show();
          $('.tranTypeError').text('* Please select a transaction type');              
          alert('Please select at least one checkbox');
          return false;            
        }
        if (valiFieldSel == false) {
          $('.secondNext').removeClass('next');
          $('.udfError').show();
          $('.udfError').text('* Please select a UDF');   
          alert('Please select at least one udf'); 
          return false;            
        }            

      $('input[name=tranType]:checked').each(function() {
          tranType.push($(this).val());
      }); 

      $.ajax({
          url: 'ajax/updateNewRule.php',
          type: 'POST',
          data: {
            id: id,
            vali_field: vali_field,
            tranType: tranType,
            ruleStep: ruleStep
          }
      })
      .done(function(data) {

      })
    })   

I have tried the above but it thinks there is something selected because one of the values has the selected attribute.

UPDATE

Heres the scenario in a fiddle - http://jsfiddle.net/csLAk/8337/

Kieron606
  • 613
  • 3
  • 13
  • 28
  • 1
    You might want to consider looking into [the `change` event](https://developer.mozilla.org/en-US/docs/Web/Events/change). This event is fired whenever a selection is made in a ` – Lix May 10 '16 at 13:38

3 Answers3

1

Save selected value on load, than on change check if value differs:

$(document).ready(function() {
  $('select').each(function() {
    $(this).data('selected', $('option:selected', this).val());
  });

  $('select').change(function() {
    if ($(this).val() != $(this).data('selected')) {
      $(this).parent().css({
        'border-color': 'green'
      });
    } else {
      $(this).parent().css({
        'border-color': 'red'
      });
    }
  })
})
div {
  float: left;
  border: 2px solid transparent;
}
select {
  width: 50px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <select>
    <option value="a">a</option>
    <option value="b">b</option>
    <option value="c">c</option>
    <option value="d">d</option>
    <option value="e">e</option>
    <option value="f" selected="selected">f</option>
    <option value="g">g</option>
    <option value="h">h</option>
    <option value="i">i</option>
    <option value="j">j</option>
    <option value="k">k</option>
  </select>
</div>
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

If I understand the question correctly, you have a value that is selected via a radio button or similar means that is selected on page load and you want to process if something other than the default value is selected. If this is so you may be able to do something like this (I am unable to test this at the moment)

Try to change:

if ($(this).is(":checked"))

to

if ($(this).is(":checked") && $(this).val() != `enter default value here`)

That should check that the logic returns false on the default value.

xec86
  • 1,291
  • 1
  • 11
  • 17
  • With your code you have to have different `if ()` for each select element. Am I right? – Justinas May 10 '16 at 13:47
  • if there is only one default value then the above code will work for everything that is not the default value, however, if there are situations where the default value is different, this is not an efficient solution. – xec86 May 10 '16 at 13:50
  • Yes, like having 5 select boxes in page... (that is common scenario) – Justinas May 10 '16 at 13:53
  • What if we make the change and click two seperate actions. http://jsfiddle.net/csLAk/8343/ – xec86 May 10 '16 at 14:20
  • 1
    Or even better what if we take the default value of the select box on page ready so that we can tell if something was selected then they reverted back to the default selection. http://jsfiddle.net/csLAk/8352/ – xec86 May 10 '16 at 14:33
-1

You can do something like this

var someVariable = false;
$('select').on('change', function() {
  someVariable = true;
});
Sam Hanley
  • 4,707
  • 7
  • 35
  • 63