我在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]
答案 0 :(得分:1)
您需要在此处使用 event delegation ,因为您的按钮已动态添加到DOM中:
$('body').on('click', '#btnOk', function() {
var url = "http://localhost:65344/Dispatch";
$(location).attr('href', url);
});
此技术可帮助您将click事件附加到此新添加的按钮。