简体   繁体   English

如何计算javascript中的变量并在html页面中打印它们

[英]How to calculate to variable in javascript and print them in the html page

I try to get result from html table witch excepted only numbers i check them throw if statement if variable 1 bigger than variable 2 print content and that works good. 我试图从html表女巫获得结果除了数字我检查他们抛出if语句如果变量1大于变量2打印内容,并且工作正常。 Now i am trying in the if statement to print variable 1 - variable 2 but it doesn't wanna work. 现在我在if语句中尝试打印变量1 - 变量2,但它不想工作。

Here is the snippet: 这是片段:

 $(document).ready(function() { var vastInkomen = 0; $('.txtBox').keyup(function() { vastInkomen = 0; $('.txtBox').each(function() { var txtBoxVal = $(this).val(); vastInkomen += Number(txtBoxVal); }); $('#vastInkomen').val(vastInkomen); writeResult(); }); var vastLasten = 0; $('.vast_lasten').keyup(function() { vastLasten = 0; $('.vast_lasten').each(function() { var vastLastenVal = $(this).val(); vastLasten += Number(vastLastenVal); }); $('#vastLasten').val(vastLasten); writeResult(); }); function writeResult() { if (vastInkomen !== 0 && vastLasten !== 0) { if (vastInkomen > vastLasten) { $('#result').text("Some text and work good!") + vastLasten - vastInkomen; } else if (vastInkomen < vastLasten) { //$('#result-amount').console(vastInkomen -vastLasten); $('#result').text("some text and work good."); } } } }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr> <td>content1</td> <td><input class="txtBox" type="number" name="content2" /> </td> </tr> <tr> <td>content3</td> <td><input class="txtBox" type="number" name="content3" /> </td> </tr> <tr> <td>content4</td> <td> <input class="txtBox" type="number" name="content4" /> </td> </tr> </table> <table> <tr> <td>content</td> <td><input class="vast_lasten" type="number" name="content" /></td> </tr> <tr> <td>content1</td> <td><input class="vast_lasten" type="number" name="content1" /></td> </tr> <tr> <td>content2</td> <td><input class="vast_lasten" type="texnumber" name="content2" /></td> </tr> </table> <div class="col"> <h3>Het resultaat is:</h3> <p id="result"></p> </div> 

This will work: 这将有效:

var result = vastLasten - vastInkomen;
$('#result').text("Some text and work good!" + result);

You can use .text() to set the content of an element, but everything should be within the parenthesis. 您可以使用.text()来设置元素的内容,但所有内容都应该在括号内。

I added an extra variable result because you cannot use + and - both here; 我添加了一个额外的变量result因为你不能在这里使用+- ; it will result in NaN as it will try to sum the values. 它将导致NaN因为它会尝试对值进行求和。

This works as well: 这也有效:

$('#result').text("Some text and work good!" + (vastLasten - vastInkomen));

EDIT: 编辑:

You have to write the variables inside the .text() brackets: 您必须在.text()括号内写入变量:

Try this: var operation = vastLasten - vastInkomen; $('#result').text("Some text and work good!" + operation); 试试这个: var operation = vastLasten - vastInkomen; $('#result').text("Some text and work good!" + operation); var operation = vastLasten - vastInkomen; $('#result').text("Some text and work good!" + operation);

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

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