JQuery:更改其他输入字段的值时更改值 [英] JQuery: Change value when value of other input field is changed

查看:97
本文介绍了JQuery:更改其他输入字段的值时更改值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我已经尝试了一段时间,而且我看起来似乎无法看到让它开始工作

 < input class =span4 input-bigid =dare_pricename =price size =30type =textonChange =updatePrice()/> 
< input class =span4 input-bigid =total_price_amountreadonly =readonlyvalue =/>


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

这里是小提琴。

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

如果您以编程方式更改这些值,则需要通过您的方法来管理它们 -

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

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


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.

解决方案

$("#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 DEMO for this event-driven approach.

这篇关于JQuery:更改其他输入字段的值时更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆