jQuery 如何添加两个文本框值并传递给另一个文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21694235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:39:47  来源:igfitidea点击:

How add the two textbox value and pass to another textbox?

jquery

提问by surname

How to add my textbox 1 and 2 value and pass to textbox 3?

如何添加我的文本框 1 和 2 值并传递给文本框 3?

<input name="1" id="1" value="" >
    <input name="2" id="2" value="" >

        <input name="3" id="3" value="" readonly>

Here's my fiddle http://jsfiddle.net/Zy46N/6/

这是我的小提琴http://jsfiddle.net/Zy46N/6/

回答by Tushar Gupta - curioustushar

Fiddle Demo

Fiddle Demo

Adding Two Strings with space separated.

以空格分隔的两个字符串相加。

var input = $('[name="1"],[name="2"]'),
    input1 = $('[name="1"]'),
    input2 = $('[name="2"]'),
    input3 = $('[name="3"]');
input.change(function () {
    input3.val(input1.val() + ' ' + input2.val());
});



两个数字相加

isNaN()

isNaN()

if It's not a valid Number take it's value 0

如果它不是有效数字,则取其值 0

Fiddle Demo

Fiddle Demo

var input = $('[name="1"],[name="2"]'),
    input1 = $('[name="1"]'),
    input2 = $('[name="2"]'),
    input3 = $('[name="3"]');
input.change(function () {
    var val1 = (isNaN(parseInt(input1.val()))) ? 0 : parseInt(input1.val());
    var val2 = (isNaN(parseInt(input2.val()))) ? 0 : parseInt(input2.val());
    input3.val(val1 + val2);
});

回答by Adil

Use class selectorfor textboxesto add, use alph numeric ids. Use parseFloatto convert textto number.

使用class selectorfortextboxes添加,使用 alph numeric ids。使用parseFloat转换textnumber

Live Demo

现场演示

$('.common').change(function () {
    $('#id3').val(parseFloat("0"+$('#id1').val()) + parseFloat("0"+$('#id2').val()));
});

回答by Farkhat Mikhalko

You can use val and change function from jquery

您可以使用 jquery 中的 val 和 change 函数

$("#1, #2").change(function(){
     var val1 = $("#1").val(),
         val2 = $("#2").val();
     $("#3").val(val1 + val2);
});

回答by MusicLovingIndianGirl

Use this in your javascript function.

在您的 javascript 函数中使用它。

var sum=$("#txtbox1").val()+$("#txtbox2").val();
// Assign sum to third textbox
$("#txtbox3").val(sum);

回答by Amit

Try this

尝试这个

$('input').change(function() {
   $('[name="3"]').val(parseInt($("#1").val())+(parseInt($("#2").val())));
});

DEMO

演示

回答by Rohan

Try this

尝试这个

    $('input').change(function() {
    if($('[name="1"]').val()!=="" && $('[name="2"]').val()!=="")
    {
   $('[name="3"]').val(parseInt($("#1").val())+(parseInt($("#2").val())));
    }
    else
    {
        $('[name="3"]').val("");
    }
});

回答by rockStar

your code itself is working and just need a little of changing.

您的代码本身正在运行,只需要进行一些更改。

$('[name="1"], [name="2"]').change(function() {
   $('[name="3"]').val($('#1').val() + $('#2').val());
});

Fiddle sample.

小提琴样本。