jquery 闭包函数
jquery 闭包函数
攀爬的小瓜牛 发表于3年前
jquery 闭包函数
  • 发表于 3年前
  • 阅读 10
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

摘要: 例----

$(function(){

var checkoutShipping = {

/*

** Choose Shipping Address 

*/

chooseAddress: function(){

$(".jq_chooseShippingAddress").on("change", function(){


var $form = $(this).closest("form");


if($(this).val() != -1) {

var shippingAddressID = $(this).val(),

data = $.parseJSON($form.find("#shippingAddressJSON").text());


var shippingAddress = data[shippingAddressID];


$form.fillForm(shippingAddress);

$form.find("#sgAddressBookId").val($(this).val());


/*$form.find(".jq_singleShippingAddressForm").disabledForm(true);*/

}else {

/*$form.find(".jq_singleShippingAddressForm").disabledForm(false);*/

$form.find("#sgAddressBookId").val("");

$form.clearForm();

$(this).val("-1");

}

defaultAddresss();

});

}

};


checkoutShipping.chooseAddress();


/*

** Show add new address popup in muliple shipping address 

*/


var MulipleShippingAdress = {

settings: {

$currentSelect: "",

$currentSelectVal: ""

},

init: function(){

this.bindEvent();

this.submitShippingAddressForm();

this.submitMultipleShippingAddressForm();

},

bindEvent: function(){

var settings = this.settings;

// show shipping address popup

$(".jq_addNewAddress").on("change", function(event){

event.preventDefault();

settings.$currentSelect = $(this);

settings.$currentSelectVal = $(this).closest(".form-group").find("var[name=selectVal]");


if($(this).val() == -1){

$("#modalShippingAddress").modal("show");

}else {

settings.$currentSelectVal.text($(this).val());

}

});

// bind model hidden event 

$("#modalShippingAddress").on("hidden.bs.modal", function(event){

event.preventDefault();


$(this).find("form").clearForm();

settings.$currentSelect.val(settings.$currentSelectVal.text());

});


// bind modal cancel event

$("#modalShippingAddress .jq_cancel").on("click", function(event){

event.preventDefault();


$(this).closest(".modal").modal("hide");

});


},

submitShippingAddressForm: function(){

var settings = this.settings;

//ajax submit

$("#saveNewAddress").on("click", function(event){

event.preventDefault();

$(this).addClass("disabled");

$(this).attr("disabled", "disabled");

var address = $("#addNewShippingAddressForm #shippingFirstName").val() + ", " +

$("#addNewShippingAddressForm #shippingLastName").val()+ ", " +

$("#addNewShippingAddressForm #shippingAddress1").val();

var $this = $(this);

//clear error message

$('#addNewShippingAddressForm').find(".has-error").removeClass("has-error");

$('#addNewShippingAddressForm').find(".form-group label.error").detach();

                $('#addNewShippingAddressForm').ajaxSubmit({

                    success: function(data){

                        data = $.parseJSON(data);

                        if(typeof(data.status) !== undefined && data.status) {

                            // add shipping address option

                            $(".jq_addNewAddress").find("option:last").before('<option value="'+data.addressId+'" >'+ address + '</option>');

                            

                            settings.$currentSelectVal.text(data.addressId);


                            $("#modalShippingAddress").modal("hide");

                        } else {

                            //set error message on popup page

                            for ( var key in data.error ){

                                var $formGroup = $("#"+key).closest(".form-group");

                                var contentKey = data.error[key];

                                var contentValue = contentKeyParser(contentKey);

                                $formGroup.addClass("has-error");

                                $formGroup.append('<label class="error">'+contentValue+'</label>');

                             }

                         }

                         $this.removeClass("disabled");

                         $this.removeAttr("disabled");

                        

                     }

                });


});

},

submitMultipleShippingAddressForm: function(){

/*

* Bind Event

* submit multiple shipping address form 

*/

$(".jq_multipleShippingAddressSubmit").on("click", function(event){

event.preventDefault();

var isSubmit = true,

errorMap = $.parseJSON($("#multipleShippingAddressForm").find("var[name=errorMessage]").text());

$("#multipleShippingAddressForm .jq_addNewAddress").each(function(){

var value = $(this).val();

if( $.trim(value).length === 0 || value == -1 ) {

var $formGroup = $(this).closest(".form-group");

$formGroup.addClass("has-error");

if($formGroup.find("label.error").size() > 0){

$formGroup.find("label.error").html(errorMap.empty);

}else {

$formGroup.append('<label class="error">'+errorMap.empty+'</label>');

}

isSubmit = false;

} else {

$(this).closest(".form-group").removeClass("has-error");

$(this).closest(".form-group").find("label.error").detach();

}

});

if(isSubmit){

$("#multipleShippingAddressForm").submit();

}

});

}

};

var defaultAddresss = function(){

var saveDefaultShippingAdressCheck = $("#saveDefaultShippingAdressCheck");

$('.form-group').on('click',saveDefaultShippingAdressCheck.parent().next(),function(){

if(saveDefaultShippingAdressCheck.attr('checked')){

saveDefaultShippingAdressCheck.val('true');

}else{

saveDefaultShippingAdressCheck.val('false');

}

});

if($('#defaultShippingGroupId').val() != undefined && $('#defaultShippingGroupId').val() != "" && $('#defaultShippingGroupId').val() == $("#sgAddressBookId").val()){

saveDefaultShippingAdressCheck.attr('checked',true).val('true').parent().next().addClass('checked');

}else{

saveDefaultShippingAdressCheck.attr('checked',false).val('false').parent().next().removeClass('checked');

}

}

/*

* Bind Event

* submit single shipping address form 

*/

$(".jq_shippingAddressSubmit").on("click", function(event){

event.preventDefault();

$("#createShippingAddressForm").submit();

});


/*

* Bind Event

* submit shipping method form 

*/

$(".jq_shippingMethodSubmit").on("click", function(event){

event.preventDefault();

$("#applyShippingMethodForm").submit();

});

/*

* Bind Event

* submit back to shipping address form

*/

$(".jq_backToShippingAddressFromMethod").on("click", function(event){

event.preventDefault();

$("#backToShippingAddressForm").submit();

});

/*

* Bind Event

* go to multiple shipping address form

*/

$(".jq_goToMultipleShippingAddressFrom").on("click", function(event){

event.preventDefault();

$("#shipToMultipleAddressForm").submit();

});

/*

* Bind Event

* go to single shipping address form

*/

$(".jq_goToSingleShippingAddressFrom").on("click", function(event){

event.preventDefault();

$("#shipToSingleAddressForm").submit();

});

/*

* Bind Event

* submit back to shipping address form

*/

$(".jq_backToCart").on("click", function(event){

event.preventDefault();

$("#backToCartFrom").submit();

});

MulipleShippingAdress.init();

    function contentKeyParser(key) {

        // because key is like xx.xx.xx so not use jquery

        var domKey = document.getElementById(key);

        var value = $(domKey).text() || "";

        return value;

    }

    

    /**

     * for highlight navigation bar

     */

    (function highlightNavigationBar() {

        var $cartridgeType = $(".jq_checkoutCartridgeType");

        var cartridgeType = $(".jq_checkoutCartridgeType").text();

        var navTab = $("." + "jq_" + cartridgeType);

        if (!navTab.hasClass("active")) {

            navTab.addClass("active");

        }

    })();

});


  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 4
博文 102
码字总数 63322
×
攀爬的小瓜牛
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: