简体   繁体   English

从选择框jQuery中获取最大值,最小值

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

Hei friends. 嘿朋友。 I've got a problem with getting the max and min value from select box(html). 我在从选择框(html)获取最大值和最小值时遇到了问题。 I want to get MAX AND MIN value from selectbox with jquery or javascript. 我想用jquery或javascript从selectbox中获得MAX AND MIN值。

Here is the markup, simple range slider. 这是标记,简单范围滑块。

$('#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 ]); 
    }
});

I want to get them from the select box, select box will be dynamicly filled by PHP. 我想从选择框中获取它们,选择框将由PHP动态填充。 Here is the code: 这是代码:

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

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


?>
</select>

Thanks for help. 感谢帮助。

Your question is ambiguous. 您的问题不明确。 Two ways of interpreting: 两种解释方式:

1. You want javascript to know the min and max value of the selectbox. 1.您希望javascript知道选择框的最小值和最大值。

You can use: 您可以使用:

 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. You want the server to know javascripts max and min value. 2.您希望服务器知道javascript的最大值和最小值。

The DOM is built by the server, ie it builds the HTML. DOM由服务器构建,即,它构建HTML。 The HTML includes the javascript which defines the min value and the max value. HTML包含定义最小值和最大值的javascript。 When data is sent back to the server via a POST, only the SET value will be send. 通过POST将数据发送回服务器时,将仅发送SET值。 Normally, the min and max value won't matter to the server. 通常,最小值和最大值对服务器无关紧要。 If you want the min and max values to be sent, you have to make hidden input fields containing those values. 如果要发送最小值和最大值,则必须制作包含这些值的隐藏输入字段。 However, why would you? 但是,为什么呢? You define the min and max value don't you? 您定义最小值和最大值不是吗?

-- edit -- -编辑-

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 ]); 
    }
});

depending on the $row["values"] format, you can get max and min value like this first get all option text into an array and pass as Int with parseInt() then do 根据$ 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);

hope it helped 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM