Javascript JQuery:更改其他输入字段的值时更改值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12287996/
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-23 07:36:51  来源:igfitidea点击:

JQuery: Change value when value of other input field is changed

javascriptjqueryhtml

提问by Anujan

I want to change the value of an input field whenever the value of another one is changed.

每当更改另一个输入字段的值时,我想更改输入字段的值。

I've tried for a while and I can't seem to get it to work

我已经尝试了一段时间,但似乎无法正常工作

<input class="span4 input-big" id="dare_price" name="price" size="30" type="text" onChange="updatePrice()" />
<input class="span4 input-big" id="total_price_amount" readonly="readonly" value=""/>?


function updatePrice() {
    var price = $("#dare_price").val();
    var total = (price + 1) * 1.05;
    $("$total_price_amount").val(total);
}?

Here's the fiddle.

这是小提琴。

采纳答案by Robin Maben

$("#dare_price").change(function(){
   var total = Number($this).val()) + ...;
   $('#total_price').val(total);
});

If you programatically change the values, you need to pipeline them through your method -

如果您以编程方式更改值,则需要通过您的方法将它们流水线化 -

function changePrice(value){
   $('#dare_price').val(value);
   $('#dare_price').trigger('change');
}

Updated DEMOfor this event-driven approach.

为这种事件驱动的方法更新了 DEMO

回答by OACDesigns

A couple of things.

几件事。

  1. Firstly I'd use the newest version of jQuery if I were you. We're on 1.8 now.

  2. I'd also use "keyup" as the event trigger as well as "change". it's just more usable in my opinion. I like to bind events using jQuery also. See http://api.jquery.com/on/

  3. there were some errors in your code. Note the $("$total_price_amount")part was throwing an error

  4. If you want to only show a set number of decimal point, use .toFixed(). But note that this will return a string.

  5. input values from text boxes are type string, so to do mathematical calculations with them you need to parse them to an intor a float

  1. 首先,如果我是你,我会使用最新版本的 jQuery。我们现在是 1.8。

  2. 我还会使用“keyup”作为事件触发器以及“change”。在我看来它更有用。我也喜欢使用 jQuery 绑定事件。见http://api.jquery.com/on/

  3. 您的代码中有一些错误。请注意该$("$total_price_amount")部件引发错误

  4. 如果您只想显示一组小数点,请使用.toFixed(). 但请注意,这将返回一个字符串。

  5. 文本框中的输入值是 type string,因此要对它们进行数学计算,您需要将它们解析为 anint或 afloat

Here's my updated JSFiddle

这是我更新的 JSFiddle

http://jsfiddle.net/DigitalBiscuits/QWSQp/71/

http://jsfiddle.net/DigitalBiscuits/QWSQp/71/

And the actual code I used

我使用的实际代码

$(document).ready(function()
{
    function updatePrice()
    {
        var price = parseFloat($("#dare_price").val());
        var total = (price + 1) * 1.05;
        var total = total.toFixed(2);
        $("#total_price_amount").val(total);
    }
    $(document).on("change, keyup", "#dare_price", updatePrice);
});

notethat I removed the onChange="updatePrice()"part from the HTML as it's no longer needed.

请注意,我onChange="updatePrice()"从 HTML 中删除了该部分,因为它不再需要。

Hope this helps you.

希望这对你有帮助。

回答by Mark Pieszak - Trilon.io

Just to be safe you should parseFloat()your price. Also, before doing any of this you should make sure it is even a number to begin with! Otherwise you'll get NaN

为了安全起见,你应该parseFloat()你的价格。此外,在做任何这些之前,你应该确保它是一个数字!否则你会得到NaN

function updatePrice() {
    var price = $("#dare_price").val();
    var total = (parseFloat(price) + 1) * 1.05;
    console.log(total);

    // Other big problem was the $total_price_amount typo that should of had # instead
    $("#total_price_amount").val(total);
}?

jsFiddle Demo

jsFiddle 演示

回答by rosscj2533

Just looks like a typo

只是看起来像一个错字

$("$total_price_amount").val(total);

Should be

应该

$("#total_price_amount").val(total);

Use the '#' to get the id

使用“#”获取id

回答by David 10K

Here an example with the change event. You can use events like keydown or -up, whatever suits to your purposes.(The live-method is used for elements created during runtime.)

这是更改事件的示例。您可以使用 keydown 或 -up 之类的事件,任何适合您的目的。(live-method 用于在运行时创建的元素。)

  $('#first').live("change", (function(){       
            $('#idofinputfield').val('mynewvalue');         
    }))