dongxin5054 2012-07-13 10:08
浏览 55
已采纳

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

Hei friends. I've got a problem with getting the max and min value from select box(html). I want to get MAX AND MIN value from selectbox with jquery or javascript.

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. 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.

  • 写回答

2条回答 默认 最新

  • duanrui3480 2012-07-13 10:17
    关注

    Your question is ambiguous. Two ways of interpreting:

    1. You want javascript to know the min and max value of the selectbox.

    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.

    The DOM is built by the server, i.e. it builds the HTML. The HTML includes the javascript which defines the min value and the max value. When data is sent back to the server via a POST, only the SET value will be send. 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 ]); 
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题