簡體   English   中英

從選擇框jQuery中獲取最大值,最小值

[英]Get max,min value from select box Jquery

嘿朋友。 我在從選擇框(html)獲取最大值和最小值時遇到了問題。 我想用jquery或javascript從selectbox中獲得MAX AND MIN值。

這是標記,簡單范圍滑塊。

$('#slider').slider({
        range: true,
        min: 0,//HERE I WANT TO GET VALUES
        max: 40,
        step: 10, // Use this determine the amount of each interval
        values: [ 20, 40 ], // The default range
        slide: function( event, ui ) {
          // for input text box
          $( "#amount" ).val( ui.values[ 0 ] + " - " + ui.values[ 1 ] );
          // Display and selected the min Price
          $( "#my_min" ).val(ui.values[ 0 ]);
          // Display and selected the max Price 
          $( "#my_max" ).val(ui.values[ 1 ]); 
    }
});

我想從選擇框中獲取它們,選擇框將由PHP動態填充。 這是代碼:

<select id="my_min" class="price_range">
<?php //dynamicly filled options 

         while($row=mysql_fetch_array($query))
            {echo "<option>".$row['values']."</option>";}


?>
</select>

感謝幫助。

您的問題不明確。 兩種解釋方式:

1.您希望javascript知道選擇框的最小值和最大值。

您可以使用:

 var options = document.getElementById('yourselectbox').childNodes;
 var min = 999999999;
 var max = 0;
 for(var i = 0; i < options.length; i++) {
     if(options[i].value > max) max = options[i].value;
     if(options[i].value < min) min = options[i].value;
 }

2.您希望服務器知道javascript的最大值和最小值。

DOM由服務器構建,即,它構建HTML。 HTML包含定義最小值和最大值的javascript。 通過POST將數據發送回服務器時,將僅發送SET值。 通常,最小值和最大值對服務器無關緊要。 如果要發送最小值和最大值,則必須制作包含這些值的隱藏輸入字段。 但是,為什么呢? 您定義最小值和最大值不是嗎?

-編輯-

var options = document.getElementById('yourselectbox').childNodes;
var min = 999999999;
var max = 0;
for(var i = 0; i < options.length; i++) {
    if(options[i].value > max) max = options[i].value;
    if(options[i].value < min) min = options[i].value;
}
$('#slider').slider({
        range: true,
        min: min,
        max: max,
        step: 10, // Use this determine the amount of each interval
        values: [ 20, 40 ], // The default range
        slide: function( event, ui ) {
          // for input text box
          $( "#amount" ).val( ui.values[ 0 ] + " - " + ui.values[ 1 ] );
          // Display and selected the min Price
          $( "#my_min" ).val(ui.values[ 0 ]);
          // Display and selected the max Price 
          $( "#my_max" ).val(ui.values[ 1 ]); 
    }
});

根據$ row [“ values”]格式,您可以像這樣首先獲得max和min值,首先將所有選項文本都放入數組中,並使用parseInt()作為Int傳遞,然后執行

OptionsArray.maximum = function(ArrayParameter){
 return Math.max.apply( Math, ArrayParameter);
};

OptionsArray.minimum = function(ArrayParameter){
 return Math.min.apply( Math, ArrayParameter);
};

console.log('Maximum is '+OptionsArray.maximum);
console.log('Minimum is '+OptionsArray.minimum);

希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM