简体   繁体   English

从 javascript 中的条件触发模态

[英]Triggering modal from condition in javascript

I have been painfully attempting to try and trigger a modal to fire, when a user writes more than 10 words in a textarea.当用户在文本区域中写入超过 10 个单词时,我一直在痛苦地尝试触发模式触发。 I have tried the bootstrap documentation, 'Toggle AND 'Show' but they don't work.我已经尝试过引导文档“切换和显示”,但它们不起作用。 My console informs me that '.modal' isn't a correct function?我的控制台告诉我“.modal”不是正确的 function? but bootstrap informs me it is!但引导程序告诉我它是!

Basically my modal does not fire when the condition is met.基本上,当条件满足时,我的模态不会触发。

Here is my code:这是我的代码:

HTML

  <form>
  <textarea placeholder="Would you like to leave a review?" maxlength="" id="textareaID"></textarea>
</form>


<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script>
<script src="/node_modules/jquery/dist/jquery.js"></script>
<script src="./script.js"></script>

JS JS

$('#textareaID').bind('input propertychange', function() {
  if (this.value.length > 10) {
    $(document).ready(function(){
    $('#exampleModal').modal('toggle');
  }
    $("#textareaID").val($("#textareaID").val().substring(0,20));
}
});

You can leverage .modal('show') .您可以利用.modal('show')

Keep in mind that adding $(document).ready(fun... inside the binding is useless. You should be wrapping all your jQuery inside it.请记住,在绑定中添加$(document).ready(fun...是没有用的。您应该将所有 jQuery 包装在其中。

Use the following:使用以下内容:

 $(document).ready(function(){ $('#textareaID').on('input propertychange', function() { if ($('#textareaID').val().length > 10) { $('#exampleModal').modal('show'); $("#textareaID").val($("#textareaID").val().substring(0,20)); } }); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"><form> <textarea placeholder="Would you like to leave a review?" maxlength="" id="textareaID"></textarea> </form> <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body">... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script> <script src="/node_modules/jquery/dist/jquery.js"></script> <script src="./script.js"></script>

Use .modal('show') with proper syntax and it's working.使用正确的语法.modal('show')并且它正在工作。 It will display modal as soon as you enter 11th character.一旦您输入第 11 个字符,它将显示模态。 You can change the condition as per your need.您可以根据需要更改条件。

Regarding the use of $(document).ready() , you should wrap most javascript stuff in this function rather than using it in the if condition.关于$(document).ready()的使用,您应该将大多数 javascript 内容包装在此 function 中,而不是在if条件下使用它。 It is just as a best practices.这只是一个最佳实践。 Not all elements would be available when the javascript is being executed.执行 javascript 时,并非所有元素都可用。

 $(document).ready(function() { $('#textareaID').on('input propertychange', function() { if (this.value.length > 10) { $('#exampleModal').modal('show'); $("#textareaID").val($("#textareaID").val().substring(0, 20)); } }); });
 <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js" integrity="sha384-ho+j7jyWK8fNQe+A12Hb8AhRq26LrZ/JpcUGGOn+Y7RsweNrtN/tE3MoK7ZeZDyx" crossorigin="anonymous"></script> <form> <textarea placeholder="Would you like to leave a review?" maxlength="" id="textareaID"></textarea> </form> <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title" id="exampleModalLabel">Modal title</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body">... </div> <div class="modal-footer"> <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <button type="button" class="btn btn-primary">Save changes</button> </div> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM