jQuery在Bootstrap模态框内进行的Ajax调用。

huangapple go评论60阅读模式
英文:

jquery ajax call inside the bootstrap modal

问题

我正在使用 bootstrap 3.3.6 版本,并且我有模态对话框代码如下打开:

<div id="myModalPositions" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">ACT</h4>
            </div>
            <div class="modal-body">
                <p>加载中...</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
            </div>
    </div>
</div>

<script type="text/javascript">
$('.openStandings').on('click', function(e){
  e.preventDefault();
  $('#myModalPositions').modal('show').find('.modal-body').load($(this).attr('data-href'));
});
</script>	

myModalPositions 中,data-href 从服务器加载一个文件,我是这样做的

所以我在 data-href = url 里面添加了代码,其中我有表单和提交,并在该 data-href 文件中添加了 ajax 代码,

每次尝试提交时,它会关闭我的模态对话框

这是 data-href url 中的代码

<form name="form1" id="form1">
<input type="text" name="name" id="name">
<button type="submit" class="btn btn-primary updateData" data-dismiss="modal">继续</button>	
</form>
<script type="text/javascript" src="../jquery/jquery-2.1.0.js"></script>
<script type="text/javascript">
	$("#form1").submit(function (event) {
		event.preventDefault();
	    var formData = $(this).serialize();
	    $.ajax({
	      type: "POST",
	      url: "proceed.cfm",
	      data: formData
	    }).done(function (data) {
	      console.log(data);
	    });
  	});
</script>	

我在这里做错了什么

英文:

I am using bootstrap 3.3.6 and i have the modal dialog code opens like this

<div id="myModalPositions" class="modal fade">
<div class="modal-dialog">
    <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title">ACT</h4>
            </div>
            <div class="modal-body">
                <p>Loading...</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            </div>
    </div>
</div>

<script type="text/javascript">
$('.openStandings').on('click', function(e){
  e.preventDefault();
  $('#myModalPositions').modal('show').find('.modal-body').load($(this).attr('data-href'));
});
</script>	

inside the myModalPositions, the data-href loads a file from the server which i am doing

so i added a code inside the data-href = url where i have the form and submit and added the ajax code inside that data-href file,

every time i am trying to submit, it is closing my modal dialog

This is the ode inside the data-href url

<form name="form1" id="form1">
<input type="text" name="name" id="name">
<button type="submit" class="btn btn-primary updateData" data-dismiss="modal">Proceed</button>	
</form>
<script type="text/javascript" src="../jquery/jquery-2.1.0.js"></script>
<script type="text/javascript">
	$("#form1").submit(function (event) {
		event.preventDefault();
	    var formData = $(this).serialize();
	    $.ajax({
	      type: "POST",
	      url: "proceed.cfm",
	      data: formData
	    }).done(function (data) {
	      console.log(data);
	    });
  	});
</script>	

what i am doing wrong here

答案1

得分: 0

您表单中的按钮具有属性 data-dismiss="modal",因此它会关闭模态框。

英文:

Your button in the form has attribute data-dismiss="modal" so it closes the modal.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;script src=&quot;https://code.jquery.com/jquery-2.2.4.min.js&quot; integrity=&quot;sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;


&lt;!-- Latest compiled and minified CSS --&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css&quot; integrity=&quot;sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu&quot; crossorigin=&quot;anonymous&quot;&gt;

&lt;!-- Latest compiled and minified JavaScript --&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js&quot; integrity=&quot;sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt;



&lt;div id=&quot;myModalPositions&quot; class=&quot;modal fade&quot;&gt;
  &lt;div class=&quot;modal-dialog&quot;&gt;
    &lt;div class=&quot;modal-content&quot;&gt;
      &lt;div class=&quot;modal-header&quot;&gt;
        &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/button&gt;
        &lt;h4 class=&quot;modal-title&quot;&gt;ACT&lt;/h4&gt;
      &lt;/div&gt;
      &lt;div class=&quot;modal-body&quot;&gt;

        &lt;form name=&quot;form1&quot; id=&quot;form1&quot;&gt;
          &lt;input type=&quot;text&quot; name=&quot;name&quot; id=&quot;name&quot;&gt;
          &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary updateData&quot; &gt;Proceed&lt;/button&gt;
        &lt;/form&gt;

      &lt;/div&gt;
      &lt;div class=&quot;modal-footer&quot;&gt;
        &lt;button type=&quot;button&quot; class=&quot;btn btn-default&quot; data-dismiss=&quot;modal&quot;&gt;Close&lt;/button&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;



&lt;button class=&quot;openStandings&quot;&gt;openStandings&lt;/button&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
  $(&#39;.openStandings&#39;).on(&#39;click&#39;, function(e) {
    e.preventDefault();
    $(&#39;#myModalPositions&#39;).modal(&#39;show&#39;).find(&#39;.modal-body&#39;).load($(this).attr(&#39;data-href&#39;));
  });


  $(&quot;#form1&quot;).submit(function(event) {
    event.preventDefault();
    console.log(&quot;sbumit&quot;);
    var formData = $(this).serialize();
    $.ajax({
      type: &quot;POST&quot;,
      url: &quot;proceed.cfm&quot;,
      data: formData
    }).done(function(data) {
      console.log(data);
    });
  });
&lt;/script&gt;

<!-- end snippet -->

答案2

得分: 0

使用 data-backdrop="static"

英文:

use data-backdrop="static"

huangapple
  • 本文由 发表于 2023年6月9日 12:00:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定