简体   繁体   English

使用JQuery简单添加功能

[英]Simple add function with JQuery

I'm trying to write a very simple function in jQuery, in which I add the input values ​​of two input-fields (type text) and output the result. 我正在尝试在jQuery中编写一个非常简单的函数,在其中我添加两个输入字段(类型文本)的输入值并输出结果。

I get either NaN or no result. 我得到NaN或没有结果。 Have tried it already with parseInt(), but also had no success. 已经用parseInt()尝试过,但也没有成功。

What am I doing wrong? 我究竟做错了什么?

 function output() { var stellplaetze = $('.stellplaetze').val(); var miete = $('.miete').val(); var result = stellplaetze * miete * 30; $('.result').html( '<strong class="text-uppercase">Ertrag pro Monat: </strong><br> ' + result + ' EUR' ); } $(function() { // Trigger the function $('button').on('click', function() { output(); $('#ertragsrechner .result').css('opacity', 1); }); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="formbody"> <input type="hidden" name="FORM_SUBMIT" value="auto_form_5"> <input type="hidden" name="REQUEST_TOKEN" value="pC9vYIP8oBzVONQiwg-ixrLwDhEdK0LA8-5n-6kQJPw"> <div class="widget widget-text stellplaetze"> <input type="text" name="stellplaetze" id="ctrl_12" class="text stellplaetze form-control flex-fill mr-0 mr-sm-2 mb-3" value="" placeholder="Anzahl der Stellplätze"> </div> <div class="widget widget-text miete"> <input type="text" name="miete" id="ctrl_13" class="text miete form-control flex-fill mr-0 mr-sm-2 mb-3" value="" placeholder="Miete pro Tag"> </div> <div class="widget widget-submit add"> <button type="submit" id="ctrl_15" class="submit add btn btn-primary">Ertrag berechnen</button> </div> <div class="widget widget-explanation explanation result"> <p>Ertrag Pro Monat</p> </div> </div> 

Actually, the issue is with the selector be more specific since there are multiple elements with the same class name. 实际上,问题在于选择器更具体,因为有多个具有相同类名的元素。 In your case, you can add input along with class selector. 在您的情况下,您可以添加input和类选择器。

By default jQuery val() method just retrieves the value of the first element in the collection, in your case, it's the div element. 默认情况下,jQuery val()方法只检索集合中第一个元素的值,在您的情况下,它是div元素。

From jQuery docs: 来自jQuery文档:

Get the current value of the first element in the set of matched elements 获取匹配元素集中第一个元素的当前值

 function output() { var stellplaetze = $('input.stellplaetze').val();stellplaetze // here --------------^^^^^^--------- var miete = $('input.miete').val(); // here -------^^^^^^--------- var result = stellplaetze * miete * 30; $('.result').html( '<strong class="text-uppercase">Ertrag pro Monat: </strong><br> ' + result + ' EUR' ); } // Trigger the function $('#ertragsrechner button').on('click', function() { output(); $('#ertragsrechner .result').css('opacity', 1); }); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="ertragsrechner"> <div class="formbody"> <input type="hidden" name="FORM_SUBMIT" value="auto_form_5"> <input type="hidden" name="REQUEST_TOKEN" value="pC9vYIP8oBzVONQiwg-ixrLwDhEdK0LA8-5n-6kQJPw"> <div class="widget widget-text stellplaetze"> <input type="text" name="stellplaetze" id="ctrl_12" class="text stellplaetze form-control flex-fill mr-0 mr-sm-2 mb-3" value="" placeholder="Anzahl der Stellplätze"> </div> <div class="widget widget-text miete"> <input type="text" name="miete" id="ctrl_13" class="text miete form-control flex-fill mr-0 mr-sm-2 mb-3" value="" placeholder="Miete pro Tag"> </div> <div class="widget widget-submit add"> <button type="submit" id="ctrl_15" class="submit add btn btn-primary">Ertrag berechnen</button> </div> <div class="widget widget-explanation explanation result"> <p>Ertrag Pro Monat</p> </div> </div> </div> 

Assuming all the variables are retrieved from an input box, while multiplying change the variables to numbers using Number() 假设从输入框中检索所有变量,同时乘以使用Number()将变量更改为数字

var result = Number(stellplaetze) * Number(miete) * 30;

There are multiple elements with same class in the html. html中有多个具有相同类的元素。 To Select the input with the class miete and stellplaetze use input .classname as the jquery selector 要使用类miete和stellplaetze选择输入,请使用输入.classname作为jquery选择器

 function output() { var stellplaetze = $('input.stellplaetze').val(); var miete = $('input.miete').val(); var result = Number(stellplaetze) * Number(miete) * 30; $('.result').html( '<strong class="text-uppercase">Ertrag pro Monat: </strong><br> ' + result + ' EUR' ); } $(function() { // Trigger the function $('button').on('click', function() { output(); $('#ertragsrechner .result').css('opacity', 1); }); }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div class="formbody"> <input type="hidden" name="FORM_SUBMIT" value="auto_form_5"> <input type="hidden" name="REQUEST_TOKEN" value="pC9vYIP8oBzVONQiwg-ixrLwDhEdK0LA8-5n-6kQJPw"> <div class="widget widget-text stellplaetze"> <input type="text" name="stellplaetze" id="ctrl_12" class="text stellplaetze form-control flex-fill mr-0 mr-sm-2 mb-3" value="" placeholder="Anzahl der Stellplätze"> </div> <div class="widget widget-text miete"> <input type="text" name="miete" id="ctrl_13" class="text miete form-control flex-fill mr-0 mr-sm-2 mb-3" value="" placeholder="Miete pro Tag"> </div> <div class="widget widget-submit add"> <button type="submit" id="ctrl_15" class="submit add btn btn-primary">Ertrag berechnen</button> </div> <div class="widget widget-explanation explanation result"> <p>Ertrag Pro Monat</p> </div> </div> 

You have the same class name on many elements, while the jQuery documentation on the val() method says 你在许多元素上有相同的类名,而val()方法的jQuery文档说

Get the current value of the first element in the set of matched elements or set the value of every matched element. 获取匹配元素集中第一个元素的当前值或设置每个匹配元素的值。

So you have either to iterate over the elements array or change the class name of the input element 因此,您要么迭代元素数组,要么更改输入元素的类名

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

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