2013-11-21 109 views
0
<form> 

    <select id="ammount"> 
     <option value="">- Select -</option> 
     <option class="100" value="0"></option> 
     <option class="200" value="5,01"></option> 
     <option class="300" value="10,01"></option> 
    </select> 

    <input class="height" id="height" name="height" value=""> 

</form> 

<script type="text/javascript"> 
    $(document).ready(function() { 

     $('form').on('change', '#height', function(){ 
      var height = "3,00"; 

      // HERE I NEED TO GRAB THE RIGHT OPTION CLASS FROM SELECT OPTIONS 
     }); 
    }); 
</script> 

我的問題是如何找到合適的選項,並搶班名期權類值?規則是應該選擇小於高度值(3,00)的選項行和大於高度值(3,00)的第一個下一個選項行。如何抓住基於變量值

因此,在這種情況下,這將是<option class="100" value="0"></option>

但如果高度是5,55正確的選擇行會<option class="200" value="5,01"></option>與同爲10,01

然而,對於身高=「10,01」,或者更正確的選擇行會<option class="300" value="10,01"></option>

我在想,找到第一個較大的值(5,01)和減1排將是正道去,但我不知道該怎麼做。

請記住,高度可以是任何值(它來自用戶鍵入的輸入框),並且動態地從db中提取選項。

+1

您還沒有元素與ID'height'表單。 – qwertynl

+0

@qwertynl謝謝,我更新了我的代碼。請現在檢查一下。 – user3018704

+0

是'.'的逗號,即小數點 – UDB

回答

1

這應該做的伎倆:

$(document).ready(function() { 

    $('form').on('change', '#height', function(){ 

     var height = "3,00"; 

     var height_num = parseFloat(height.replace(',','.')); 
     var selected_item_value; 
     var heights = []; 
     $('#ammount option').each(function(i, item){ 
      heights.push(item); 
     }); 
     $(heights.reverse()).each(function(i, item){ 
      if (parseFloat($(item).val().replace(',','.')) < height_num) { 
       alert($(item).val()) 
       selected_item_value = $(item).val(); 
       return false; 
      } 
     }); 
     $('#ammount').val(selected_item_value); 
    }); 
}); 
+1

嗯,它並不適用。你可以嘗試在js小提琴或其他什麼東西來證明它真的有效嗎?提前致謝。 – user3018704

+0

woops,忘了使用item.value而不是$(item).val()...應該立即工作。 –

0
<form> 
<select id="amount"> 
    <option value="">- Select -</option> 
    <option class="100" value="0">0</option> 
    <option class="200" value="5,01">5,01</option> 
    <option class="300" value="10,01">10,01</option> 
</select> 
<input class="height" id="height" name="height" value="55"> 
</form> 

腳本

var t; 

$('#height').focus(function() { 

var that = $(this) 

t = setInterval(function() { 

    var height = parseFloat(that.val().replace(',', '.')) 


    $('#amount').children('option').each(function() { 

     var compareTo = parseFloat($(this).val().replace(',', '.')) 

     var compareToNext = 0 

     if ($(this).next().length > 0) 
      compareToNext =parseFloat($(this).next().val().replace(',', '.')) 

     if (compareTo < height && height < compareToNext && !isNaN(height)) { 
      $('#amount').val($(this).val()) 

     } 

     else if (compareTo < height && $(this).is(':last-child') && !isNaN(height)) { 
      $('#amount').val($(this).val()) 
     } 

     else if (isNaN(height)) { 
      $('#amount').val($('#amount').children('option:first').val()) 
     } 



    }) //end of each 
}, 500) //end of setInterval 
}) //end of focus 

$('#height').blur(function() { 
    window.clearInterval(t) 
}) 

DEMO