2016-10-19 25 views
-3

我想要一個輸入框只接受類型爲(20-40)的值,就像20-40之間的單個數字一樣,我告訴用戶要定義該特定輸入框的範圍?如何設置文本框接受值作爲範圍?

任何人都可以幫助我做同樣的事情。謝謝。

example of my requirement

+1

請說明您的具體問題或添加額外的細節,突顯正是你需要的。正如目前所寫,很難確切地說出你在問什麼。 –

+0

我已經添加了我的要求圖片@Paulie_D –

回答

0

有一個名爲number輸入元素類型。希望這可能對你有所幫助。

它的屬性

  1. 最小 - 最小值
  2. 馬克斯 - 最大值
  3. 步驟 - 增量爲。

<input type="number" min="10" max="20" step="2" value="10"/>

+0

我的要求不是那一個。例如 –

+0

紅色 - 0-40綠色41-80白色81-100這樣的文本框應該接受 –

0

在HTLM5有設定範圍界限的選項。 請檢查該https://jsfiddle.net/ozgsn5pn/2/

<h3>A demonstration of how to access a Slider Control</h3> 
 
<input type="range" id="myRange" min="20" max="40" value="30"> 
 
<p>Click the button to get the value of the slider control.</p> 
 
<button onclick="myFunction()">Try it</button> 
 
<p><strong>Note:</strong> input elements with type="range" are not supported in IE 9 and earlier versions.</p> 
 
<p id="demo"></p>

+0

使用角度js我們如何獲得? –

0

喜試試這個代碼來驗證輸入框範圍從20到40

<script> 
    function validate() { 
    var n= document.getElementById("txtnum").value; 
    if(!(n >= 19&& n <= 41)) 
     document.getElementById("txtnum").value=""; 
     alert("please enter valid no from 20-40"); 
    } 
    </script> 
    <input type="number" id="txtnum" onblur="validate();"> 
0

嘗試是這樣的(#20-40):

JavaScript:

function myFunction() { 
     var x = document.getElementById("input").value; 
     if (x<20 || x>40) { 
     document.getElementById("warning").innerHTML = "Choose a number between 20 and 40"; 
     } else document.getElementById("warning").innerHTML = " "; 
    } 

HTML

Example: <input id="input" onchange="myFunction()" placeholder="Choose number between 20 and 40"> 

<p id="warning"></p> 
0

function checkrange()  
 
{ 
 
    var input=parseInt(document.getElementById("number").value); 
 
    //alert(input); 
 
    if(input<=10) 
 
    { 
 
     // alert('hi'); 
 
     document.getElementById("mySelect").value = "one"; 
 
     //document.getElementById("mySelect").selectedIndex="1"; 
 
     
 
     
 
    } 
 
    else if((input>10)&&(input<20)) 
 
    { 
 
     //alert('h') 
 
     document.getElementById("mySelect").value = "two"; 
 
    } 
 
    
 
    else if((input>20)&&(input<30)) 
 
    { 
 
     //alert('h') 
 
     document.getElementById("mySelect").value = "three"; 
 
    } 
 
    else 
 
    { 
 
     alert('Invalid Range') 
 
     document.getElementById("mySelect").value = "empty"; 
 
    } 
 

 

 
}
<form> 
 
Please enter a number in textfield : 
 
<select id="mySelect"> 
 
    <option value="empty"></option> 
 
    <option value="one">1-10</option> 
 
    <option value="two">11-20</option> 
 
    <option value="three">21-30</option> 
 
    
 
</select> 
 
<input type="number" id="number" onkeyup="checkrange();" placeholder="Enter a number between 1-30" min="1" max="30" style="width: 200px;"> 
 
    </form>

嗨,

感謝張貼的問題。

我對你的答案有一個不同的方法,可能你也可以採用它。

我所做的是,用戶必須輸入一個值或一個數字,並根據該值選擇框選項更改。

運行上面的代碼片段。可能會有所幫助。

感謝和快樂編碼:)