Javascript 在模式对话框中设置默认值
我想在模态对话框打开后立即在窗体控件中设置一个默认值,不幸的是,这似乎不是那么容易。目前我不知道为什么它没有设置任何值 HTMLJavascript 在模式对话框中设置默认值,javascript,angularjs,twitter-bootstrap-3,bootstrap-modal,Javascript,Angularjs,Twitter Bootstrap 3,Bootstrap Modal,我想在模态对话框打开后立即在窗体控件中设置一个默认值,不幸的是,这似乎不是那么容易。目前我不知道为什么它没有设置任何值 HTML <!-- Modal - Create --> <div class="modal fade" id="add_new_task_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog"
<!-- Modal - Create -->
<div class="modal fade" id="add_new_task_modal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Add item</h4>
</div>
<div class="modal-body">
<ul class="alert alert-danger" ng-if="errors.length > 0">
<li ng-repeat="error in errors">
{{ error }}
</li>
</ul>
<div class="form-group">
<label for="quantity">Quantity</label>
<input ng-model="task.quantity" type="text" id="quantity" class="form-control"/>
</div>
<div class="form-group">
<label for="item">Item</label>
<input ng-model="task.item" type="text" id="item" class="form-control autocomplete"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="button" class="btn btn-primary" ng-click="addTask()">Save</button>
</div>
</div>
</div>
</div>
&时代;
添加项
-
{{error}}
量
项目
取消
拯救
JS
<script type="text/javascript">
$('#add_new_task_modal').on('show.bs.modal', function() {
$('#quantity').val("some default value");
});
</script>
$('#add_new_task_modal')。on('show.bs.modal',function(){
$('#quantity').val(“某些默认值”);
});
我找到了一个解决方案:
$(document).on('show.bs.modal','#add_new_task_modal', function () {
$('#quantity').val("asd");
})
如果使用angularjs,我会避免使用jquery。虽然它可能会起作用,但它有点违背了最初使用angular的整个想法 相反,应使用模态对话框的控制器设置初始模型状态,如下所示:
$scope.task = {
quantity: 5,
item: 'test'
}
您可以尝试模式事件“show.bs.modal”而不是show.bs.modal事件,该事件在模式完全显示(CSS转换完成后)时发生。参考链接:谢谢你的回复。不幸的是,没有什么不同。你可以创建一个小提琴和分享链接,这可能有助于回答它。非常感谢!这是正确的答案。Javascript设置了这些值,但不知怎么的,它不太可靠,并且有一些奇怪的副作用。