简体   繁体   English

jQuery:如果……其他逻辑和调用函数,我们如何实现

[英]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: 现在我正在尝试做三件事:

  1. if there is no 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 如果cart中没有content ,并且addToCart而不是执行some action ,那么基本上在这里,我们需要检查cart的当前状态,该状态是通过调用getCartItems获得的,如果它为Null并且如果addToCart则执行some action
  2. if there is 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
  3. if there is 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: 但是,由于必须在addToCartremoveFromCart时执行这些操作,因此一个简单的解决方案是将这些条件放入函数本身中:

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.

相关问题 if/else 调用函数,其余逻辑相同 - if/else to call the function, the rest of the logic is identical 如何在jQuery中实现“ OR”逻辑而不是“ AND”? - How to implement “OR” logic instead of “AND” in jQuery? 我们如何在jQuery Datatable调用结果内部而不是页面就绪函数中启动另一个jQuery插件? - How can we initiate another jQuery plugin inside jQuery Datatable call results instead of page ready function? 我该如何实现这个逻辑 - How can I implement this logic 我们可以使用切换来代替if / else和隐藏/显示逻辑吗 - can we use toggle instead of the if/else and hide/show logic 如果在运行应用程序后动态变化,如何从 Locator 中选择 ID,我们如何为其实现逻辑? - How to select ID from Locator if it's changing dyanamically after running application, How we can implement logic for it? 如何使这个未命名的函数成为我们可以调用的通用函数? - How to make this unnamed function to general function that we can just call? 如何在 JQuery 中实现多个 if/else 语句? - How to implement a multiple if/else statements in JQuery? 实现jQuery的PHP逻辑 - Implement PHP logic to jQuery 我们如何根据Java脚本If-else条件调用Php函数? - How can we call Php functions based on the Java script If-else condition?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM