Javascript 使用jquery添加两个变量
嗨,朋友们,我有两个变量price1和price2,我正在使用onchange事件动态获取price值,现在我想添加这两个变量Javascript 使用jquery添加两个变量,javascript,jquery,Javascript,Jquery,嗨,朋友们,我有两个变量price1和price2,我正在使用onchange事件动态获取price值,现在我想添加这两个变量 //get support layer firmness price $('.support-layer-firmness').on('change', function(e) { //first price var price1 = 300; }); $('.support-layer-thickness').on('change', fu
//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
//first price
var price1 = 300;
});
$('.support-layer-thickness').on('change', function(e) {
//second price
var price2 = 200;
});
Now I want to add both variables price1 & price2
eg price = price1+price2;
o/p price= 500;
如何实现这一点。将变量设置为
全局
,并在相应的更改事件中设置值,然后您可以在任何位置添加值
$(document).ready(function(){
var price1=0;
var price2=0;
$('.support-layer-firmness').on('change', function(e) {
//first price
price1 = 300;
});
$('.support-layer-thickness').on('change', function(e) {
//second price
price2 = 200;
alert(price1 + price2); //add price
});
//or you can calculate them on button click
$('#btnAdd').click(function(){
alert(price1 + price2);
});
});
将变量设置为
global
,并在相应的更改事件中设置值,然后可以在任何位置添加值
$(document).ready(function(){
var price1=0;
var price2=0;
$('.support-layer-firmness').on('change', function(e) {
//first price
price1 = 300;
});
$('.support-layer-thickness').on('change', function(e) {
//second price
price2 = 200;
alert(price1 + price2); //add price
});
//or you can calculate them on button click
$('#btnAdd').click(function(){
alert(price1 + price2);
});
});
您需要为这两个函数定义全局变量,如
var price=0,price1=0,price2=0;
//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
//first price
price1 = 300;
price=price1+price2;
alert(price);
});
$('.support-layer-thickness').on('change', function(e) {
//second price
price2 = 200;
price=price1+price2;
alert(price);
});
您需要为这两个函数定义全局变量,如
var price=0,price1=0,price2=0;
//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
//first price
price1 = 300;
price=price1+price2;
alert(price);
});
$('.support-layer-thickness').on('change', function(e) {
//second price
price2 = 200;
price=price1+price2;
alert(price);
});
可从外部进行各种访问。您必须在事件句柄之外声明变量(price1和price2)
var price1,price2; //<= declare here
//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
//first price
price1 = 300;
});
$('.support-layer-thickness').on('change', function(e) {
//second price
price2 = 200;
});
var价格1,价格2// 可从外部进行各种访问。您必须在事件句柄之外声明变量(price1和price2)
var price1,price2; //<= declare here
//get support layer firmness price
$('.support-layer-firmness').on('change', function(e) {
//first price
price1 = 300;
});
$('.support-layer-thickness').on('change', function(e) {
//second price
price2 = 200;
});
var价格1,价格2//您想计算更改时的总价吗?或手动使用价格2=200;而非var价格2=200;和1相同。一旦更改,我将获得两个变量的价格,现在我想添加它,然后我可以将总价附加到其他位置。在函数外部定义变量price1
和price2
,使其成为全局变量。然后在onChange
事件中调用一个函数来更新价格,例如updatePrice=function(){return price1+price2;}
是否要计算更改时的总价格?或手动使用价格2=200;而非var价格2=200;和1相同。一旦更改,我将获得两个变量的价格,现在我想添加它,然后我可以将总价附加到其他位置。在函数外部定义变量price1
和price2
,使其成为全局变量。然后在onChange
事件中调用一个函数来更新价格,例如updatePrice=function(){returnprice1+price2;}