Jquery点击事件的问题

时间:2014-03-07 07:10:17

标签: jquery asp.net-mvc button click entity-framework-4.1

我在MVC中实现了一个应用程序。我使用jquery创建了messagebox。当我点击按钮消息框时出现。

<button   type="button" class="btn btn-primary" id="btnDispatch" >Dispatch</button>

我的jquery代码是

$(document).ready(function () {


        $('#btnDispatch').click(function () {

            $('body').append('<div id="dataConfirmModal" class="modal" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true" id="btnClose"> × </button> <h5 id="dataConfirmLabel">Dispatch Alert</h5> </div><div class="modal-body"><h5>This order is dispatched.</h5><br/></div><div class="modal-footer"><button id="btnOk" type="submit" class="btn btn-primary" data-dismiss="modal" aria-hidden="true" >OK</button> </div></div> <div class="modal-backdrop"></div>');


            $('#dataConfirmModal').find('.modal-body').text($(this).attr('data-confirm'));

            $('#dataConfirmModal').modal({ show: true }); 

            $('#btnClose').dialog('close');

            $('#btnOk').click(function () {

                var url = "http://localhost:65344/Dispatch";
                $(location).attr('href', url);

            });
        });


    });

点击ok按钮后,我想返回发送的索引页面。但是ok按钮和x按钮在这里不起作用。 有什么解决方案吗? ![在此输入图像说明] [1]

1 个答案:

答案 0 :(得分:1)

您需要在此处使用 event delegation ,因为您的按钮已动态添加到DOM中:

$('body').on('click', '#btnOk', function() {
    var url = "http://localhost:65344/Dispatch";
    $(location).attr('href', url);
});

此技术可帮助您将click事件附加到此新添加的按钮。

相关问题