在引导程序中单击关闭时隐藏模式
Hiding a modal on clicking off in bootstrap
我有一个打开和关闭连接的按钮。当按钮处于打开状态并且用户单击它时。为了关闭它,会弹出一个模式并要求确认。但是当按钮处于关闭状态时,不应弹出任何模式。问题是它与切换为显示"开"和"关"的按钮相同,并且附加了一个模态元素。即使单击关闭按钮,也会弹出我不想要的模式。请帮帮我。
if(status == 'On'){
var url = "/streams/status";
$('button[id="modal-'+ streamId + '"]').click(function(){
console.log("close modal function")
$('myModal-'+ streamId).modal('hide');
$.ajax({
type: "POST",
url: url,
data: "streamId="+streamId,
success: function(res){
$('button[id="profile-'+ res.streamId + '"]').toggleClass('btn-success btn-danger');
$('button[id="profile-'+ res.streamId + '"]').text(function (){
console.log($(this).text())
return 'Off';
});
},
error: function (res){
console.log('there was an error', res);
}
});
})
}
else{
console.log("button is off currently");
$('myModal-' + streamId).modal('hide');
var url = "/streams/status";
$.ajax({
type: "POST",
url: url,
data: "streamId="+streamId,
success: function(res){
$('button[id="profile-'+ res.streamId + '"]').toggleClass('btn-success btn-danger');
$('button[id="profile-'+ res.streamId + '"]').text(function (){
console.log($(this).text())
return 'On';
});
},
error: function (res){
console.log('there was an error', res);
}
});
}
})
我认为您应该添加一个 rel 属性(关闭或打开),检测 onClick 事件并检查此属性:
$('#myModal-' + streamId).click(function(event){
if($('#myModal-' + streamId).attr('rel') == 'on'){
//do ajax request ON here
//on success ajax, replace return 'off' by
$('#myModal-' + streamId).attr('rel', 'off');
}
else{
//do ajax request OFF here
//on success ajax, replace return 'on' by
$('#myModal-' + streamId).attr('rel', 'on');
}
}
它应该是有效的
这是正确的方法:
script.
function toggleStatus (streamId, statusBefore){
$.ajax({
type: "POST",
url: "/streams/status",
data: "streamId="+streamId,
success: function(res){
$('button[id="profile-'+ res.streamId + '"]')
.toggleClass('btn-success btn-danger')
.attr('data-toggle', statusBefore == true ? "": 'modal');
$('#profile-glyphicon-'+ res.streamId).toggleClass('glyphicon-ok glyphicon-remove');
},
error: function (res){
console.log('there was an error', res);
}
});
}
$('button[id^="modal-"]').click(function(){
var streamId = this.id.split('-')[1];
toggleStatus(streamId, true);
$('myModal-'+streamId).modal('hide');
})
$('button[id^="profile-"]').on("click", function(e){
var streamId = this.id.split('-')[1];
var statusBefore;
var dataToggleStatus = $('#profile-' + streamId).attr("data-toggle");
if(dataToggleStatus=='modal'){
statusBefore = true;
e.preventDefault();
} else {
statusBefore = false;
toggleStatus(streamId, statusBefore);
}
})
相关文章:
- Bootstrap在页面加载缓慢时会立即显示隐藏的模式对话框
- Codemirror:将特定的基于模式的文本隐藏到编辑器中
- 如何在模式窗口弹出窗口中隐藏滚动条
- 在引导程序中单击关闭时隐藏模式
- 关闭模式覆盖时无法隐藏消息框
- 视频通过javascript被隐藏,但已经开始播放(自动播放模式),即使是's不可见
- 从日期选择器引导框中选择日期后,模式隐藏
- jquery ui模式框中的表单隐藏后,键盘将不再被调用
- 如何将自定义隐藏/关闭链接添加到使用 lightbox2 显示的模式框
- HTML5网站在丝绸浏览器中进入全屏模式或在加载URL后在丝绸浏览器中隐藏地址栏
- 带有代码隐藏的 Jquery 模式
- 可靠地隐藏引导模式
- 隐藏上的引导模式不会触发
- 如果用户单击“返回”,如何在上一页上隐藏引导模式
- 在表单提交时隐藏模式
- 不使用关闭按钮隐藏模式窗口
- AngularJS-可以ng显示/ng隐藏模式窗口
- 不要隐藏模式,而点击左/右叠加
- 引导 v2.3.2 - 隐藏模式,然后使用新内容重新打开
- 显示弹出窗口时在模式弹出窗口中打印内容,但如果隐藏模式弹出窗口则打印正文