简体   繁体   English

jQuery按钮单击事件不调用

[英]jquery button click event not invoking

i have a button group in from wizard ...save and cancel button show when wizard reached at final step. 我在向导中有一个按钮组...在向导最后一步到达时,保存并取消按钮显示。 this code in separate js code file that is use multiple time. 此代码位于单独的js代码文件中,该文件需要多次使用。

here is the js file 这是js文件

$(document).ready(function () {
//for form wizard
// Step show event
$("#smartwizard").on("showStep", function (e, anchorObject, stepNumber, stepDirection, stepPosition) {
    //alert("You are on step "+stepNumber+" now");
    if (stepPosition === 'first') {
        $("#savecancle").hide();
        $("#prev-btn").addClass('disabled');
    } else if (stepPosition === 'final') {
        $("#savecancle").show().css("float", "right");
        $("#next-btn").addClass('disabled');
    } else {
        $("#prev-btn").removeClass('disabled');
        $("#next-btn").removeClass('disabled');
    }
});

// Toolbar extra buttons
var btnFinish = $('<button></button>').text('Save')
                                 .addClass('btn btn-info').attr("id","save")
                                 .on('click', function () { /*alert('Finish Clicked');*/ });
var btnCancel = $('<button></button>').text('Cancel')
                                 .addClass('btn btn-danger')
                                 .on('click', function () { $('#smartwizard').smartWizard("reset"); });

// Smart Wizard 1
$('#smartwizard').smartWizard({
    selected: 0,
    theme: 'arrows',
    transitionEffect: 'fade',
    showStepURLhash: false,
    toolbarSettings: {
        toolbarPosition: 'bottom',
        toolbarExtraButtons: [btnFinish, btnCancel]
    }
});

}); });

when i click on save button the event dose not fired. 当我单击“保存”按钮时,事件未触发。 here is my click button code in .cshtml file 这是我在.cshtml文件中的单击按钮代码

$("#save").click(function () {
                    var isAllValid = true;
                    //Save if valid
                    if (isAllValid) {
                        var data = {
                            tblOrderDetailSubs: orderItems
                        }

                        $(this).val('Please wait...');

                        $.ajax({
                            url: '/Order/OrderAdd',
                            type: "POST",
                            data: data,
                            success: function (d) {
                                if (d.status == true) {
                                    alert('Successfully done.');
                                    //clear form
                                    orderItems = [];
                                }
                                else {
                                    alert('Failed');
                                }
                                $('#save').val('Save');
                            },
                            error: function () {
                                alert('Error. Please try again.');
                                $('#save').val('Save');
                            }
                        });
                    }

                });

can anyone help me ..? 谁能帮我 ..? thanks in advance. 提前致谢。

The button might not be in the document yet at the moment you execute: 执行该按钮时,该按钮可能尚未出现在文档中:

$("#save").click(function () {
    ...
});

Use event delegation to be sure that the event is captured once the button is available: 使用事件委托来确保一旦按钮可用就捕获事件:

$("#smartwizard").on("click", "#save", function () {
    ...
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM