更改输入值 onclick 按钮 - 纯 javascript 或 jQuery [英] Change input value onclick button - pure javascript or jQuery
问题描述
我有两个按钮,如果我点击某个按钮,我需要更改输入文本中的值并更改总价(产品价格 * 按钮值 - 2 或 4 个数量).
I have two buttons and if I click on some button I need to change value in input text and change total price (product price * value of button - 2 or 4 Qty).
我知道这很简单,但我不擅长 javascript 或 jQuery.jsfiddle 中的答案最好.
我的 jsfiddle 在这里 http://jsfiddle.net/J7m7m/1/
I know that it's simple but I'm not good in javascript or jQuery. Answer in jsfiddle would be best.
My jsfiddle is here http://jsfiddle.net/J7m7m/1/
我的简单代码:
Product price: $500
<br>
Total price: $500
<br>
<input type="button" onclick="change()" value="2
Qty">
<input type="button" value="4
Qty">
<br>
Total <input type="text" id="count" value="1">
推荐答案
另一种使用 jQuery 的简单解决方案.请记住,使用内联 javascript 不是一个好习惯.
Another simple solution for this case using jQuery. Keep in mind it's not a good practice to use inline javascript.
我已经在 html 中添加了总价和按钮的 ID.这是 jQuery.
I've added IDs to html on the total price and on the buttons. Here is the jQuery.
$('#two').click(function(){
$('#count').val('2');
$('#total').text('Product price: $1000');
});
$('#four').click(function(){
$('#count').val('4');
$('#total').text('Product price: $2000');
});
这篇关于更改输入值 onclick 按钮 - 纯 javascript 或 jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!