简体   繁体   English

如何在执行JavaScript时防止引导程序模式关闭

[英]how to prevent the bootstrap modal to close when performing a javascript

I'm generating the textbox using the number of user input in bootstrap modal. 我正在使用引导模态中的用户输入数量来生成文本框。 But when I click the button and generate a textbox the modal automatically closing and the webpage is reloading. 但是,当我单击按钮并生成一个文本框时,模式会自动关闭,并且网页正在重新加载。 How to prevent that? 如何预防呢? I'm trying to use data-backdrop="static" data-keyboard="false" on modal but not working. 我正在尝试在模式上使用data-backdrop =“ static” data-keyboard =“ false”,但无法正常工作。 Here's code: 这是代码:

<div class="form-group">
 <label class="control-label col-sm-2">Answer options :</label>
 <div class="col-sm-4" style="margin-right: -120px;">
   <input type="number" class="form-control" id="count_option" placeholder="Enter number of options" >
 </div>
 <div class="col-sm-2" >
   <button style="margin-left: 7em;" onclick="Generate(count_option.value)">Generate</button>
 </div>
</div>

<div id="options"></div>

Javascript code: JavaScript代码:

<script type="text/javascript">
  function Generate(nums){
    var str = "";
    for(var index = 1; index <= nums; index++ ){
      str += '<div class="form-group"><label class="control-label col-sm-2"></label><div class="col-sm-2" style="margin-right: -120px;"><input type="radio" name="correct" value="" onclick="getTextValue('+ index +')" id="rd' + index +'"  style="display: inline; position: absolute; margin-top: 10px;"></div><div class="col-sm-8" ><input type="text" class="form-control" id="txt' + index +'" name="ans[]" placeholder="Answer"></div></div>';
    }
    document.getElementById("options").innerHTML = str;
  }
</script>

Please help me. 请帮我。 Thanks. 谢谢。

As it is form so any button under it will behave as submit button by default and onClick , it will submit the form 因为它是form ,因此任何button下会表现为submit button默认和onClick ,它会submitform

 function stopFormSubmission(){ let btn = document.querySelector('#yourBtn') btn.addEventListener("click", function(event){ event.preventDefault() // prevent form from submitting Generate(nums) // Call your function }); } function Generate(nums){ // Your function var str = ""; for(var index = 1; index <= nums; index++ ){ str += '<div class="form-group"><label class="control-label col-sm-2"></label><div class="col-sm-2" style="margin-right: -120px;"><input type="radio" name="correct" value="" onclick="getTextValue('+ index +')" id="rd' + index +'" style="display: inline; position: absolute; margin-top: 10px;"></div><div class="col-sm-8" ><input type="text" class="form-control" id="txt' + index +'" name="ans[]" placeholder="Answer"></div></div>'; } document.getElementById("options").innerHTML = str; } 
 <form> <button id="yourBtn" onClick="stopFormSubmission()" >click</button> </form> 

You just need to prevent the default behaviour and stop form to get submitted 您只需要阻止默认行为并停止form即可提交

You can try this via change on html or script below 您可以通过以下html或脚本更改尝试

<div class="form-group">
 <label class="control-label col-sm-2">Answer options :</label>
  <div class="col-sm-4" style="margin-right: -120px;">
 <input type="number" class="form-control" id="count_option" placeholder="Enter number of options" >
</div>
<div class="col-sm-2" >
<button style="margin-left: 7em;" data-value="count_option.value" id="getValue">Generate</button>
</div>
</div>
<div id="options"></div>

i have changed in your button tag and script to follow 我已经更改了您的按钮标签和脚本以进行后续操作

<script>
document.getElementById("getValue").addEventListener("click", function(event){
    event.preventDefault();
    var str = "";
   var nums = this.getAttribute("data-value");
   for(var index = 1; index <= nums; index++ ){
      str += '<div class="form-group"><label class="control-label col-sm-2"></label>
<div class="col-sm-2" style="margin-right: -120px;"><input type="radio" name="correct" value="" onclick="getTextValue('+ index +')" id="rd' + index +'"  style="display: inline; position: absolute; margin-top: 10px;"></div><div class="col-sm-8" >
<input type="text" class="form-control" id="txt' + index +'" name="ans[]" placeholder="Answer"></div></div>';
    }
    document.getElementById("options").innerHTML = str;
}});
    </script>

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

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