Bootstrap切换隐藏的Div和输入值

时间:2018-08-31 11:48:16

标签: javascript jquery html css toggle

我正在使用Bootstrap Toggle来切换div的可见性,我可以做很多。

在div内有输入,如果用户切换时输入中有任何个值,我想用“您确定要删除这些值”框提示它们。

  • 如果用户选择ok,我想清除输入值并隐藏div,即将所有内容重置为默认值。
  • 如果用户选择cancel,我什么也不想做,即保持div打开并保留当前输入值

我似乎无法正常工作。

我的代码位于fiddle下方。

HTML

<input type="checkbox" value='1' name="test" id="test">
<hr>
<div id="advanced">
  <form>
    <input type="text" name="one">
    <input type="text" name="two">
  </form>
</div>

JQuery

function checkValues() {
  if ($('#advanced').find('input:text').val() !== '') {
    clearInput();
  }
}

function clearInput() {
  var txt;
  var r = confirm("You sure you want to remove these values?");
  if (r == true) {
    resetValues();
  } else {
    console.log('keep values and box open');
  }
}

function resetValues() {
  $('#advanced').find('input:text').val('');
}

$(function() {
  $('#test').bootstrapToggle({
    on: 'Hide',
    off: 'Show',
    onstyle: 'danger'
  });
  $("#advanced").hide();
  $('#test').change(function() {
    if ($(this).prop('checked') == true) {
      checkValues();
      $("#advanced").show(500);
    }
    if ($(this).prop('checked') == false) {
      checkValues();
      $("#advanced").hide(500);
    }

  });
})

感谢您的帮助。

3 个答案:

答案 0 :(得分:0)

尝试这个。

    function checkValues() {
  if ($('#advanced').find('input:text').val() !== '') {
    clearInput();
  } else {
    $("#advanced").hide();
  }
}

function clearInput() {
  var txt;
  var r = confirm("You sure you want to remove these values?");
  if (r == true) {
    resetValues();
    $("#advanced").hide(500);
  } else {
    $('#test').bootstrapToggle('on');
    console.log('keep values and box open');
  }
}

function resetValues() {
  $('#advanced').find('input:text').val('');
}

$(function() {
  $('#test').bootstrapToggle({
    on: 'Hide',
    off: 'Show',
    onstyle: 'danger'
  });
  $("#advanced").hide();
  $('#test').change(function() {
    if ($(this).prop('checked') == true) {
      $("#advanced").show(500);
    }
    if ($(this).prop('checked') == false) {
      checkValues();
    }

  });
})

这里是链接Link

答案 1 :(得分:0)

您可以设置某种标志,告诉系统仅在确认对话框未返回false时删除值

    function checkValues() {
      if ($('#advanced').find('input:text').val() !== '') {
        var state = clearInput();
        return state;
      }
    }

    function clearInput() {
      var txt;
      var r = confirm("You sure you want to remove these values?");
      if (r === true) {
        resetValues();
      } else {
        return false;
      }
    }

    function resetValues() {
      $('#advanced').find('input:text').val('');
    }

    $(function() {
      $('#test').bootstrapToggle({
        on: 'Hide',
        off: 'Show',
        onstyle: 'danger'
      });
      $("#advanced").hide();
      $('#test').change(function() {
        if ($(this).prop('checked') == true) {
          checkValues();
          $("#advanced").show(500);
        }
        if ($(this).prop('checked') == false) {
          state = checkValues();
          if(state != false){
              $("#advanced").hide(500);
          } else {
              $("#test").data('bs.toggle').on(true);
              // let the checkbox remain toggled
          }
          
        }

      });
    })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap-toggle.min.css" rel="stylesheet">
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<input type="checkbox" value='1' name="test" id="test">
<hr>
<div id="advanced">
  <form>
    <input type="text" name="one">
    <input type="text" name="two">
  </form>
</div>

答案 2 :(得分:0)

您在这里使用解决方案https://jsfiddle.net/Lg9p417t/64/

$(function() {
  $('#test').bootstrapToggle({
    on: 'Hide',
    off: 'Show',
    onstyle: 'danger'
  });
  $("#advanced").hide();
  $('#test').change(function() {
    if ($(this).prop('checked') == true) {
      if ($('#advanced').find('input:text').val() === '') {
        $('#advanced').find('input:text').val('');
      }
      $("#advanced").show(500);
    }
    if ($(this).prop('checked') == false) {
      if ($('#advanced').find('input:text').val() !== '') {
        var txt;
        var r = confirm("You sure you want to remove these values?");
        if (r == true) {
          $("#advanced").hide(500);
          $('#advanced').find('input:text').val('');
        } else {
          $('#test').bootstrapToggle('on');
        }
      } else {
        $("#advanced").hide(500);
      }
    }
  });
})
<link href="https://gitcdn.github.io/bootstrap-toggle/2.2.2/css/bootstrap2-toggle.min.css" rel="stylesheet"/>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.0.4/css/bootstrap.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://gitcdn.github.io/bootstrap-toggle/2.2.2/js/bootstrap-toggle.min.js"></script>
<script src="http://bootstrapdocs.com/v2.0.4/docs/assets/js/bootstrap-button.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.0.4/bootstrap.min.js"></script>
<script src="https://rawgit.com/eu81273/jsfiddle-console/master/console.js"></script>

<input type="checkbox" value='1' name="test" id="test">
<hr>
<div id="advanced">
  <form>
    <input type="text" name="one">
    <input type="text" name="two">
  </form>
</div>

相关问题