Don't like ads? PRO users don't see any ads ;-)
Guest

how to check if a selection field is selected

By: a guest on Mar 26th, 2012  |  syntax: None  |  size: 1.12 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <select class="select" id="quantity" name="quantity">
  2. <option selected="selected" value="">select one</option>
  3. <option value="data1">data1</option>
  4. <option value="data2">data2</option>
  5. <option value="data3">data3</option>
  6. </select>
  7.        
  8. $(document).ready(function() {
  9.  
  10.     $("#btnaddcart").click(function()
  11.     {
  12.  
  13.          if($("input[name=quantity]").val() == null)
  14.           {
  15.                var data = $(this).val();
  16.                alert(data);
  17.           }
  18.       });
  19. });
  20.        
  21. if($("input[name=quantity]").val() == null)
  22.        
  23. if($('#quantity').val() == '')
  24.        
  25. # not selected
  26. if (!$("#quantity option:selected").length) {
  27.  
  28. }
  29.        
  30. $(document).ready(function() {
  31.     $("#btnaddcart").click(function(){
  32.         var box=document.getElementById('quantity');
  33.         var index=box.selectedIndex;
  34.         // any one selected apart from the first option
  35.         if(index!=0){
  36.              var data=box.options[index].value;
  37.              alert(data);
  38.         }
  39.     });
  40. });
  41.        
  42. $("#btnaddcart").click(function()
  43. {
  44.      if($("#quantity").val() == '')
  45.       {
  46.            alert("cannot be null");
  47.       }
  48.     else{
  49.            alert($(this).val());
  50.     }
  51. });