在引导程序中单击时隐藏模态

时间:2013-12-18 15:08:16

标签: javascript jquery html twitter-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);
     }
   });

  }
  })

2 个答案:

答案 0 :(得分:0)

我认为你应该添加一个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');
  }
}

它应该是有效的

答案 1 :(得分:0)

这是正确的方法:

    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);
  }
})