[英]jQuery: How can we implement if…else logic and call function
I am new to jQuery and so don't mind this question if it sounds stupid but here is something that I am trying to do : 我是jQuery的新手,所以如果听起来很蠢,就不要介意这个问题,但这是我要尝试做的事情:
I have 3 functions like: 我有3个功能,例如:
AddToCart Function which adds item to the shopping cart:
//offer_id is the offer which we are trying to add to cart.
addToCart: function(offer_id)
{
},
RemoveFromCart which removes data from the cart
//target is link clicked and event is the click event.
removeFromCart: function(target, event)
{
},
Get the current state of the cart
//return string which represents current state of cart.
getCartItems: function()
{
}
Now I am trying to do 3 things: 现在我正在尝试做三件事:
content
in cart and addToCart
is called than some action
, so basically here we need to check the current state of cart and that is obtained by calling getCartItems
and if it is Null
and than if addToCart
is called than we perform some action
content
,并且addToCart
而不是执行some action
,那么基本上在这里,我们需要检查cart的当前状态,该状态是通过调用getCartItems
获得的,如果它为Null
并且如果addToCart
则执行some action
content
in the cart and addToCart
is called than some action
,so basically here we need to check the current state of cart and that is obtained by calling getCartItems
and check if it is Null
or not and than if addToCart
is called than we perform some action
if we had some content in the cart. content
并且addToCart
而不是执行some action
,那么基本上在这里,我们需要检查购物车的当前状态,该状态是通过调用getCartItems
获得的,并检查它是否为Null
以及是否比我们调用addToCart
如果购物车中有一些内容,请执行some action
。 content
in the cart and removeFromCart
is called some action
, so basically here we need to check the current state of cart and that is obtained by calling getCartItems
and if it is not Null and if removeFromCart
is called than we perform some action
content
,并且removeFromCart
被称为some action
,那么基本上在这里我们需要检查购物车的当前状态,这是通过调用getCartItems
获得的,如果它不是Null,并且如果调用removeFromCart
则我们将执行some action
Pseudocode of what I am trying to do: 我正在尝试做的伪代码:
if there is no content in cart
and addToCart is called than
$(document).track(
);
if there is content in the cart
and addToCart is called than
$(document).track(
);
if there is content in the cart
and removeFromCart is called
$(document).track(
);
My basic concern is that am complete newbie to jQuery and JavaScript and so am not sure how can I implement if...else logic and how can I call a function using jQuery/JavaScript. 我的基本担心是对jQuery和JavaScript完全是新手,因此不确定如何实现if ... else逻辑以及如何使用jQuery / JavaScript调用函数。
It's similar to any other programming language. 它与任何其他编程语言相似。
if() {
..
}
else if() {
..
}
else if() {
..
}
However, since these actions have to be performed when addToCart
and removeFromCart
are called, a simple solution is to put those conditions inside the functions itself: 但是,由于必须在
addToCart
和removeFromCart
时执行这些操作,因此一个简单的解决方案是将这些条件放入函数本身中:
addToCart: function(offer_id) {
// add to cart is called and cart IS empty
if(this.getCartItems().length == 0) {
}
// add to cart is called and cart is NOT empty
else {
}
}
removeFromCart: function(target, event) {
// remove was called and cart has items
if(this.getCartItems().length > 0) {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.