简体   繁体   English

如何使用表格输入数字作为Javascript变量?

[英]How do I use a form input number as a Javascript variable?

<form>
  <input type="text" name="" value="">
</form>
//for loop used to calculate balance after payment(x) and interest
// the variable I want defined from the form input box
for(i = 6000; i>=10; i = i * (1+0.2/26)-x){
    var x = 155;
    document.write("Balance " + " $" + i + "<br/><br/>");
}

You could attach a pseudo class in your input element and then get the value inserted like below: 您可以在输入元素中附加一个伪类,然后获取插入的值,如下所示:

<input type="text" name="" value="" class="js-interest">

<script>
    var ele = document.getElementsByClassName("js-interest")[0];
    var value = parseFloat(ele.value);
</script>

You can try document.getElementById('input_id').value and then use parseInt to get it as an integer, as below: 您可以尝试document.getElementById('input_id').value ,然后使用parseInt将其作为整数获取,如下所示:

var x = parseFloat(document.getElementsByName('number_box').value);

Also, the html must look something like this: 另外,html必须看起来像这样:

<form>
    <input type="text" name="number_box" value="">
</form>

Optionally, instead of document.getElementsByName() you can use document.getElementById() or document.getElementsByClassName() . (可选document.getElementsByName()可以代替document.getElementsByName()来使用document.getElementById()document.getElementsByClassName()

Update: If I am not wrong 更新:如果我没看错

    for(i = 6000; i>=10; i = i * (1+0.2/26)-x){
       var x = 155;
       document.write("Balance " + " $" + i + "<br/><br/>");
    }

It seems like you are writing to the DOM inside a for loop don't do that calculate your ANS and then write to the DOM. 似乎您正在for循环内写入DOM,请不要这样做来计算您的ANS,然后再写入DOM。

Also, don't read the data from input inside the loop. 同样,不要从循环内部的输入中读取数据。 (You will be repeatedly reading and writing the data thats not good.) (您将反复读取和写入不好的数据。)

Your for loop for(i = 6000; i>=10; i = i * (1+0.2/26)-x) is incrementing using some expression i = i * (1+0.2/26)-x (make sure it is bound to the condition and its not making the loop infinite) 您的for循环for(i = 6000; i>=10; i = i * (1+0.2/26)-x)正在使用某些表达式i = i * (1+0.2/26)-x (请确保绑定到条件,并且不会使循环无限长)

You can select the value from the input field using the following code 您可以使用以下代码从输入字段中选择值

x = parseInt(document.querySelector('form input[name="my_number"]').value);

document.querySelector it uses CSS style selector. document.querySelector它使用CSS样式选择器。 So, to select the input field inside your form. 因此,要选择表单内的输入字段。 I have added a name to the form as name="my_number" 我已经在表单中添加了一个名称,即name="my_number"

<input type="text" name="my_number" value="">

now using the css selector form input[name="my_number"] it select the input field inside a form with name "my_number" 现在使用CSS选择form input[name="my_number"]它选择一个表格内的输入字段名称为"my_number"

The whole Query selector that will return the input element is this, 返回输入元素的整个查询选择器是这个,

document.querySelector('form input[name="my_number"]')

now to get the value of the input field you have to read the value property of the input field. 现在,要获取输入字段的值,您必须读取输入字段的value属性。

document.querySelector('form input[name="my_number"]').value

This will return your input value as string. 这将以字符串形式返回您的输入值。

Now, we need to parse that string value to a Integer format. 现在,我们需要将该字符串值解析为Integer格式。

We do that like this, (using parseInt ) 我们这样做(使用parseInt

parseInt(document.querySelector('form input[name="my_number"]').value)

I have added you code in a function named calc 我在名为calc的函数中添加了代码

  function calc() {
     var x = parseInt(document.querySelector('form input[name="my_number"]').value);

    var ans= calculate_your_value(x);


    document.write("Balance " + " $" + ans + "<br/><br/>");
}

I have fetched the answer from a function named get calculated answer and its good to do this way. 我已经从一个名为“获取计算的答案”的函数中获取了答案,这样做很好。

function calculate_your_value(x){
  // calculate the ans the for loop seems buggy
  //for (i = 6000; i >= 10; i = i * (1 + 0.2 / 26) - x) {}

  return x; //dummy ans
}

It is called when you submit the form. 提交表单时会调用它。

To do that I have added onsubmit='calc()' on your form tag. 为此,我在表单标签上添加了onsubmit='calc()'

<form onsubmit='calc()'>

Additionally, I have added this function that submits the form when you have pressed enter too (Just for fun) :) 此外,我还添加了此功能,当您也按Enter键时,该功能可以提交表单(只是为了好玩):)

document.onkeydown=function(){
    if(window.event.keyCode=='13'){
        calc();
    }
}

It just listens for key down press and check if it is a enter key (keycode is 13) 它只是监听按键按下并检查它是否是Enter键(键码为13)

and calls the same calc function. 并调用相同的calc函数。

 function calc() { var x = parseInt(document.querySelector('form input[name="my_number"]').value); var ans= calculate_your_value(x); document.write("Balance " + " $" + ans + "<br/><br/>"); } document.onkeydown = function() { if (window.event.keyCode == '13') { calc(); } } function calculate_your_value(x){ // calculate the ans the for loop seems buggy //for (i = 6000; i >= 10; i = i * (1 + 0.2 / 26) - x) {} return x; //dummy ans } 
 <form onsubmit='calc()'> <input type="text" name="my_number" value=""> <input type="submit" id="submitbtn" /> </form> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM