选择距离半径搜索javascript的选项

时间:2018-12-11 20:43:52

标签: javascript jquery

嗨,我正在尝试添加一个选项下拉列表,该下拉列表可以更改英里半径,例如:25英里,50英里,100英里,并将更新所选半径范围内的位置。这是什么

 var newOptions = {
  "Option 1": 1200,
   "Option 2": 1400,
   "Option 3": 160934
  };

  var $el = $("#selectId");
   $el.empty(); // remove old options
   $.each(newOptions, function(key,value) {
   $el.append($("<option></option>").attr("value", value).text(key));
   });




    $('#selectId').change(function(){
      filterLocationsWithinSetDistance();
     });


    var withinDistanceSetting =$("#selectId option").val();; //meters


       function filterLocationsWithinSetDistance {
        if (val.distance <=  withinDistanceSetting ) { //html output 
           of customers in this range }}

我需要它来更新输出功能(filterLocationsWithinSetDistance) 希望这样做,因为如果您需要更多信息,请让我知道。谢谢

1 个答案:

答案 0 :(得分:1)

您需要在用户更改下拉列表并进行过滤时从下拉列表中获取值。页面加载时,您的代码即可获取它。

您需要获取下拉菜单本身的值。 $("#selectId option").val()返回第一个选项的值,而不是用户选择的选项。

var newOptions = {
  "Option 1": 1200,
  "Option 2": 1400,
  "Option 3": 160934
};

var $el = $("#selectId");
$el.empty(); // remove old options
$.each(newOptions, function(key, value) {
  $el.append($("<option></option>").attr("value", value).text(key));
});

$('#selectId').change(function() {
  filterLocationsWithinSetDistance();
});

function filterLocationsWithinSetDistance() {
    var withinDistanceSetting = $("#selectId").val();
    console.log("Distance being filtered = " + withinDistanceSetting);
    var values = []; // get elements we're processing
    $.each(values, function(i, val) {
        if (val.distance <=  withinDistanceSetting ) { 
            // code that processes this element
        }
    });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select id="selectId"></select>