本文介绍了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);
}


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:更改其他输入字段的值时更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 02:53
查看更多