【问题标题】:How to avoid refreshing the page in a bootstrap modal?如何避免在引导模式中刷新页面?
【发布时间】:2013-02-03 00:01:27
【问题描述】:

这是我在 twitter bootstrap 中的模态代码:

<div class="modal-body">
<form class="form-horizontal" method="POST" name="loginForm" id="loginForm">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
  <div class="input-prepend">
    <span class="add-on"><i class="icon-envelope"></i></span>
    <input class="span2" id="inputIcon" type="text" name="email"/>
  </div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
  <div class="input-prepend">
    <span class="add-on"><i class="icon-lock"></i></span>
    <input class="span2" id="password" type="password" name="password"/>
  </div>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
</form>
<button type="submit" class="btn" id="signin_button">Sign in</button>
</div>
</div>

每当我按下登录按钮时,它都会重新加载页面。单击按钮时如何禁用刷新页面?

【问题讨论】:

    标签: jquery ajax codeigniter twitter-bootstrap


    【解决方案1】:

    您可以阻止提交页面的按钮的默认行为。

    这里是“preventDefault”上 Mozilla 开发者网络的链接:event.preventDefault

    在 jQuery 中你可以这样做:

    $("#signin_button").on("click", function(e) {
        e.preventDefault();
    
        // the rest of your code ...
    });
    

    【讨论】:

    • 谢谢!你为我节省了大量时间!
    【解决方案2】:

    尝试将输入 type="submit" 更改为

    <button class="btn" id="signin_button">Sign in</button> 
    

    <input type="button" class="btn" id="signin_button" value="Sign in"/>
    

    如果您的目标不是提交表单。

    也可能有用:Difference between <input type='button' /> and <input type='submit' />

    【讨论】:

    • 我必须包含 'type="button" ' ...否则它仍然会尝试提交。
    【解决方案3】:

    你也可以使用点击:

    $("#signin_button").click(function (e) {
      e.preventDefault();
      // code
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 2012-06-12
      相关资源
      最近更新 更多