简体   繁体   English

用户在输入字段中输入价格..选择具有自动范围的框

[英]user enters price in input field .. select box with auto range

Ok. 好。 Still busily figuring out our form, etc. So scenario. 仍在忙于弄清楚我们的表格,等等。 We have a form for member to enter a price. 我们有一个表格供会员输入价格。 Typically, they would enter anything between 50,000 and 10,000,000. 通常,他们会输入50,000到10,000,000之间的任何值。 We also have select drop-down, with value ranges. 我们还具有带有值范围的选择下拉列表。

Example of the form so far: 到目前为止的表格示例:

<div class="field">
  <label for="propertyprice">Price</label>
  <input id="currency" name="limitedtextfield" size="50" type="text" class="medium" onKeyPress="return numbersonly(event, false)" onKeyDown="limitText(this.form.limitedtextfield,this.form,8);" 
onKeyUp="limitText(this.form.limitedtextfield,this.form,8);" maxlength="8"/>
  <p class="field_help">Enter only numbers.</p>
</div>

<div class="field">
  <label for="propertypricerange">Price Range </label>
  <select id="propertypricerange" name="propertypricerange" class="medium">
    <optgroup label="Enter Price Range">
    <option selected="selected" value="0balance">0    -    $100,000</option>
    <option value="100kmin">$100,000    -    $200,000</option>
    <option value="200kmin">$200,000    -    $300,000</option>
    <option value="300kmin">$300,000    -    $400,000</option>
    <option value="400kmin">$400,000    -    $500,000</option>
    <option value="500kmin">$500,000    -    $600,000</option>
    <option value="600kmin">$600,000    -    $700,000</option>
    <option value="700kmin">$700,000    -    $800,000</option>
    <option value="800kmin">$800,000    -    $900,000</option>
    <option value="900kmin">$900,000    -    $1,000,000</option>
    <option value="1milmin">$1,000,000    -    $2,000,000</option>
    <option value="2milmin">$2,000,000    -    $3,000,000</option>
    <option value="3milmin">$3,000,000    -    $4,000,000</option>
    <option value="4milmin">$4,000,000    -    $5,000,000</option>
    <option value="5milmin">$5,000,000    -    $10,000,000</option>
    <option value="10milplus">$10,000,000    -    +</option>
    </optgroup>
  </select>
</div>

What I would like to do, is the select price ranges automatically select the range that the users types in. There is no need to disable the select ranges, because we don't always need member to type in a price in input field, but we would like them to then select a range. 我想做的是选择价格范围自动选择用户输入的范围。不需要禁用选择范围,因为我们并不总是需要成员在输入字段中输入价格,但是我们希望他们然后选择一个范围。 So a kinda vice versa form. 反之亦然。

Anyone ever done anything like this? 有人做过这样的事情吗?

This works on your current code for values up to 9: 这适用于您当前的代码,最大值为9:

function findRange(e) {
   !e ? e = window.event : null;
   if(e.keyCode >=48 && e.keyCode <=57) {
     $('propertypricerange').selectedIndex = e.keyCode - 48;
   }
 }

 $('propertypricerange').onkeyup = findRange;

With values for 1 million and above, the user will have to type in more than one key, and also the Javascript will have to remember more than one key, so you may need an input box for that. 如果值等于或大于一百万,则用户将不得不输入多个键,而Javascript将不得不记住多个键,因此您可能需要输入框。 If you don't want an input box, here's a variation on the code above that will remember as multiple values and select that index (eg, type "1", "2" and you'll see index 12, backspace, you'll see index 1) 如果您不想使用输入框,则可以在上面的代码中进行修改,以记住多个值并选择该索引(例如,键入“ 1”,“ 2”,然后会看到索引12,退格,请参阅索引1)

 var val = [];
 function findRange(e) {
   !e ? e = window.event : null;
   if(e.keyCode==8) {
     val.pop();
     $('propertypricerange').selectedIndex = val.join("");
   }

   if(e.keyCode >=48 && e.keyCode <=57) {
     val.push(e.keyCode - 48);
     $('propertypricerange').selectedIndex = val.join("");
   }
 }

 $('propertypricerange').onkeyup = findRange;

I would recommend you store the range values in a javascript var - if your ranges will never intersect, you can just use an array of the start values. 我建议您将范围值存储在javascript var中-如果您的范围永远不会相交,则可以只使用一组起始值。 Then, every time the onchange event is fired for the input box - 然后,每次为输入框触发onchange事件时-

  • Parse the value of the input box to a number 将输入框的值解析为一个数字
  • Loop through the array of start points until you find a value which is greater than your parsed number and subtract 1 from the index. 循环遍历起点数组,直到找到一个大于解析数字的值,然后从索引中减去1。
  • Change the selectedindex of the select box to the appropriate index. 将选择框的selectedindex更改为适当的索引。

Easy peasy... 十分简单...

Rename your option IDs to something like this: 重命名您的选项ID,如下所示:

<input type="text" id="text-value" value="0" name="text-value" />
  <select id="select-list" name="select-list">
    <option id="min_100" value="100kmin">$100,000    -    $200,000</option>
    <option id="min_200" value="200kmin">$200,000    -    $300,000</option>
    ...
  </select>

$(function(){
  $('#select-list').change(function(){
    var _id = $(this).val().split('_');
    $('#text-value').val('$'+_id[1]+',000');
  });
});

Searching for the right selection can use the same method. 搜索正确的选择可以使用相同的方法。

Full rewrite here. 全文在这里重写。 Supports number keys, numpad, and multiple-character entry, with backspace to remove. 支持数字键,数字键盘和多字符输入,并带有退格键。 Entering 2-0-backspace will give you 200,000 - 2,000,000 - 200,000. 输入2-0退格键将为您提供200,000-2,000,000-200,000。 Works in IE and FF. 可在IE和FF中使用。 Does not need input field, just focus the drop down and type. 不需要input字段,只需关注下拉菜单并输入即可。

HTML, values adjusted for ease in Javascript: HTML,为简化JavaScript调整的值:

<optgroup label="Enter Price Range">
  <option selected="selected" value="0balance">0    -    $100,000</option>
  <option value="1">$100,000    -    $200,000</option>
  <option value="2">$200,000    -    $300,000</option>
  <option value="3">$300,000    -    $400,000</option>
  <option value="4">$400,000    -    $500,000</option>
  <option value="5">$500,000    -    $600,000</option>
  <option value="6">$600,000    -    $700,000</option>
  <option value="7">$700,000    -    $800,000</option>
  <option value="8">$800,000    -    $900,000</option>
  <option value="9">$900,000    -    $1,000,000</option>
  <option value="10">$1,000,000    -    $2,000,000</option>
  <option value="20">$2,000,000    -    $3,000,000</option>
  <option value="30">$3,000,000    -    $4,000,000</option>
  <option value="40">$4,000,000    -    $5,000,000</option>
  <option value="50">$5,000,000    -    $10,000,000</option>
  <option value="100">$10,000,000    -    +</option>
</optgroup>

Javascript: Javascript:

//Keypress storage
var val = [];

//Keypress manager function
function findRange(e) {
  !e ? e = window.event : null;
  $('out').innerHTML = e.keyCode   // SORRY REMOVE THIS LINE (testing only)

  //Enable backspacing
  if(e.keyCode==8) {
    val.pop();
    findIndex();
  }

  //Enable keyboard numbers
  if(e.keyCode >=48 && e.keyCode <=57) {
    val.push(e.keyCode - 48);
    findIndex();
  }

  //Enable numpad numbers
  if(e.keyCode >=96 && e.keyCode <=105) {
    val.push(e.keyCode - 96);
    findIndex();
  }
}

//Dropdown option value finder
function findIndex() {
  var opts = $('propertypricerange').options;
  for(x=0;x<opts.length;x++) {
    if(opts[x].value == val.join("")) {
      $('propertypricerange').selectedIndex = x;
      break;
    }
  }
}

//Event
$('propertypricerange').onkeyup = findRange;

EDIT 编辑

Accepted solution actually refers to this , from the comments. 接受的解决方案实际上是从注释中引用

EDIT: Here's a non-looping version. 编辑:这是一个非循环版本。 Simply uses arithmetic. 简单地使用算术。

Example: http://jsfiddle.net/uv8Jh/4/ 示例: http //jsfiddle.net/uv8Jh/4/

var sel = $('#propertypricerange')[0];

function findRange(val) {
    return ( val < 1000000 ) ? (val - val % 100000) / 100000 :
           ( val < 10000000 ) ? Math.min((val - val % 1000000) / 1000000 + 9, 14) :
            15;    
}
$('#currency').keyup(function() {
    sel.selectedIndex = findRange(this.value.replace(/[^0-9.]/g,''));
});

Original Answer: 原始答案:

Here's one solution, though it may need a little code to ensure that only numbers are entered into the input. 这是一个解决方案,尽管它可能需要一些代码来确保仅数字输入到输入中。

Doesn't rely on keycodes. 不依赖于键码。 Just compares the input value to the values that have been stored in an array. 只需将输入值与已存储在数组中的值进行比较即可。

Example: http://jsfiddle.net/uv8Jh/1/ 示例: http //jsfiddle.net/uv8Jh/1/

   // cache the select list
var sel = $('#propertypricerange')[0];
   // Make an array of the values
var values = $.map( sel.options, function(opt) {
    return (opt.value.indexOf('kmin') > -1) ?
        parseInt(opt.value) * 1000 :
        parseInt(opt.value) * 1000000;
});
  // receive the current value and return the index of the proper range
function findRange( val ) {
    var i = 0, len = values.length;
    while( i++ < len ) {
        if( val < values[ i ] ) {
            return i-1;
        }
    }
    return len - 1;
}
   // set the proper range on keyup event
$('#currency').keyup(function() {
    sel.selectedIndex = findRange( this.value );
});

Again, there should be some cleanup in case the user types an invalid character. 同样,应该进行一些清理,以防用户键入无效字符。 Simple regex should do it. 简单的正则表达式应该做到这一点。


EDIT: Here's the key event with a little checking for invalid characters. 编辑:这是关键事件,需要一点点检查无效字符。

Example: http://jsfiddle.net/uv8Jh/2/ 示例: http //jsfiddle.net/uv8Jh/2/

$('#currency').keyup(function() {
    sel.selectedIndex = findRange(this.value = this.value.replace(/[^0-9.]/g,''));
});

暂无
暂无

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

相关问题 用户输入后如何获取输入字段的值 - How to get the value of input field as soon as user enters it javascript:在输入框中输入数量后自动计算价格 - javascript: Auto calculate price upon entering quantity in input box 如何使用jQuery文本框中提供的输入选择价格范围? - How can I select Price range with Input provided in textbox in jquery? 在用户输入输入后,使用jquery使用autosuggest填写输入文本字段 - using autosuggest to fill in an input text field after user enters their input, using jquery 当用户在输入字段中输入数据时,如何从把手页面获取 jquery 中的输入文本值? - How to get the input text value in jquery from handlebar page when user enters data in input field? 输入字段出现在默认选中的选择框上 - Input field appearing on default checked select box 选择框自动选择 - select box auto select React JS用户在单击按钮时在输入字段中输入值以显示值 - React JS user enters value in the input field on button click to display the value 当用户在文本输入字段中输入过滤器文本时,jquery过滤器脚本不会忽略变音符号并且不突出显示字符串匹配项 - jquery filter script not ignoring diacritics and not highlighting string matches as user enters filter text into text input field 填充数据列表花费的时间太长。 用户在输入字段中输入几个字母后如何开始填充 - Populating datalist takes too long. How to start populating after user enters a few letters in the input field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM