查询将两个输入字段添加在一起
我有以下代码。我想将两个输入字段添加在一起并将其输出到页面。我尽可能输出输入字段中的类型,但是我不知道如何将两个字段添加在一起并输出它。我在 http://jsfiddle.net/erick/9Dj3j/3/
Jquery
$(function() {
var output_element = $('#output_ele');
var output_element1 = $('#output_ele1');
$('#the_input_id').keyup(function() {
var their_input = $(this).val();
var firstInput = output_element.text(their_input);
});
$('#the_input_id1').keyup(function() {
var their_input1 = $(this).val();
var firstInput1 = output_element1.text(their_input1);
});
var output_total = $('#total');
var total = firstInput + firstInput1;
output_total.text(total);
});
HTML
<form method="post">
<input type="text" id="the_input_id">
<input type="text" id="the_input_id1">
</form>
<div id="output_ele">
</div>
<div id="output_ele1">
</div>
<div id="total">
</div>
I have the following code. I want to add two input fields together and output it to the page. I get as far as being able to output what is type in the input field however I can't figure how to add the two fields together and output it. I have it at http://jsfiddle.net/erick/9Dj3j/3/
Jquery
$(function() {
var output_element = $('#output_ele');
var output_element1 = $('#output_ele1');
$('#the_input_id').keyup(function() {
var their_input = $(this).val();
var firstInput = output_element.text(their_input);
});
$('#the_input_id1').keyup(function() {
var their_input1 = $(this).val();
var firstInput1 = output_element1.text(their_input1);
});
var output_total = $('#total');
var total = firstInput + firstInput1;
output_total.text(total);
});
HTML
<form method="post">
<input type="text" id="the_input_id">
<input type="text" id="the_input_id1">
</form>
<div id="output_ele">
</div>
<div id="output_ele1">
</div>
<div id="total">
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的代码由于多种原因不起作用
keyUp
上,您将新值分配给本地的firstInput
和firstInput1
总价值是在这些回调未定义
)您需要什么要做的就是编写一个函数,将两个值相加并调用来自
keyUp
处理程序小提琴: http://jsfiddle.net/9Dj3j/56/
The code you posted isn't working for a couple of reasons
keyUp
you assign the new values tofirstInput
andfirstInput1
which are local to the callbackundefined
)What you need to do is write a function that adds the two values together and call that from the
keyUp
handlerFiddle: http://jsfiddle.net/9Dj3j/56/
我对上面的答案做了一些修改,这是最终的 javascript:
Fiddle: http://jsfiddle.net/ 9Dj3j/259/
I did a little modification on the answer above and here is the final javascript:
Fiddle: http://jsfiddle.net/9Dj3j/259/