[英]javascript text field value
I want to calculate even and odd numbers through a text field,the text field is the range where I put values ,and I select even and odd through select tag.So how am I supposed to get the value from text field to calculate function? 我想通过一个文本字段计算偶数和奇数,该文本字段是我放置值的范围,并且我通过select标签选择了偶数和奇数。那么我应该如何从文本字段获取值以计算函数呢?
JavaScript Code JavaScript代码
function calculate()
{
var i=0; for(i=1;i<=1000;i++)
{
if(i%2==0) {
document.getElementById("even").value="the numbers is" + i + "<br>";
}
else {
document.getElementById("odd").value="the numbers is" + i + "<br>"; }
}
}
HTML HTML
<body>
<input type="text" name="value" value="" id="value1" placeholder="Enter the
first number" />
<select>
<option value="even" id="even">Even</option>
<option value="odd" id="odd">Odd</option>
</select>
<p id="value1"> </p>
<p id="even"> </p>
<p id="odd"> </p>
<input type="button" value="Calculate" onclick="calculate()" />
Refer to this page Input Text value Property - w3schools 参见本页输入文本值属性-w3schools
To get value from input field you can use : 要从输入字段中获取价值,您可以使用:
var x = document.getElementById("myText").value;
To set value in input field you can use : 要在输入字段中设置值,可以使用:
document.getElementById("myText").value = "my value";
Setters: 塞特斯:
document.getElementById("yourInputField").value = "Value"; // JavaScript
$('#yourInputField').val(Value); // jQuery
Getters: 干将:
var Value = document.getElementById("yourInputField").value; // JavaScript
var Value = $('#yourInputField').val(); // jQuery
Use them in your calculation method, and enjoy. 在您的计算方法中使用它们,然后享受。 ;-) ;-)
from what I understand you need to pick value of your text box but you will also need a function that gets hit on key press and passes your textbox value to calculate() function 根据我的理解,您需要选择文本框的值,但是您还需要一个函数,该函数会在按键时被击中,并将您的文本框值传递给calculate()函数
use this function that will get executed once you press a key. 使用此功能,一旦按下一个键,该功能便会执行。
$("#yourtextboxidhere").keyup(function(){
var textValue = $.trim( $('#yourtextboxidhere').val() );
// convert the string value from text box to int so you can use it in your calculate function for comparison
// as text box values are string)
var textValue = parseInt(textValue)
//Put your calculate function here and pass it the value:
calculate(textValue);
//calculate function goes here.... (don't forget to add a parameter in you calculate function so you may use it in a correct way)
//replace 'i' in your function with the passed parameter.
});
don't forget to use jquery. 不要忘记使用jQuery。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.