我有一个bootstrap 2.32模式,我正在尝试将其动态插入另一个视图的HTML中。我正在使用Codeigniter 2.1。在将引导模式部分视图动态插入视图之后,我有:
<div id="modal_target"></div>
作为插入的目标div。我的视图中有一个按钮,看起来像:
<a href="#message" class="btn btn-large btn-info" data-action="Message" data-toggle="tab">Message</a>
我的JS有:
$.ajax({
type : 'POST',
url : "AjaxUpdate/get_modal",
cache : false,
success : function(data){
if(data){
$('#modal_target').html(data);
//This shows the modal
$('#form-content').modal();
}
}
});
主视图的按钮是:
<div id="thanks"><p><a data-toggle="modal" href="#form-content" class="btn btn-primary btn-large">Modal powers, activate!</a></p></div>
以下是我想单独存储的部分视图:
<div id="form-content" class="modal hide fade in" style="display: none;">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Send me a message</h3>
</div>
<div class="modal-body">
<form class="contact" name="contact">
<fieldset>
<!-- Form Name -->
<legend>modalForm</legend>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="user">User</label>
<div class="controls">
<input id="user" name="user" type="text" placeholder="User" class="input-xlarge" required="">
<p class="help-block">help</p>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="old">Old password</label>
<div class="controls">
<input id="old" name="old" type="text" placeholder="placeholder" class="input-xlarge">
<p class="help-block">help</p>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="new">New password</label>
<div class="controls">
<input id="new" name="new" type="text" placeholder="placeholder" class="input-xlarge">
<p class="help-block">help</p>
</div>
</div>
<!-- Text input-->
<div class="control-group">
<label class="control-label" for="repeat">Repeat new password</label>
<div class="controls">
<input id="repeat" name="repeat" type="text" placeholder="placeholder" class="input-xlarge">
<p class="help-block">help</p>
</div>
</div>
</fieldset>
</form>
</div>
<div class="modal-footer">
<input class="btn btn-success" type="submit" value="Send!" id="submit">
<a href="#" class="btn" data-dismiss="modal">Nah.</a>
</div>
</div>
单击按钮时,模态被正确插入,但出现以下错误:
TypeError: $(...).modal is not a function
我怎样才能解决这个问题?
我只想强调mwebber在评论中所说的:
还检查jQuery是否不包括两次
:)
这是我的问题
此错误实际上是由于在调用modal
函数之前未包含引导程序的javascript。模态是在引导中定义的。js不是jQuery。还需要注意的是,bootstrap实际上需要jQuery来定义modal
函数,因此在包含bootstrap的javascript之前包含jQuery是至关重要的。为了避免错误,只需确保在调用modal
函数之前包含jQuery,然后是bootstrap的javascript。我通常在标题标签中执行以下操作:
<header>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="/path/to/bootstrap/js/bootstrap.min.js"></script>
</header>
链接jquery和引导程序后,在jquery和引导程序的脚本标记下方编写代码。
$(document).ready(function($) {
$("#div").on("click", "a", function(event) {
event.preventDefault();
jQuery.noConflict();
$('#myModal').modal('show');
});
});
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>