[英]Setting default values in a modal dialog
我想在模式对话框打开后立即在窗体控件中设置一个默认值,不幸的是,这似乎并不那么容易。 目前,我不知道为什么它没有设置任何值。
HTML
<!-- 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>
JS
<script type="text/javascript">
$('#add_new_task_modal').on('show.bs.modal', function() {
$('#quantity').val("some default value");
});
</script>
如果您为此使用angularjs,我将避免使用jquery。 尽管可能会起作用,但它首先与使用angular的整个想法背道而驰。
相反,您应该使用模式对话框的控制器来设置初始模型状态,如下所示:
$scope.task = {
quantity: 5,
item: 'test'
}
我找到了解决方案:
$(document).on('show.bs.modal','#add_new_task_modal', function () {
$('#quantity').val("asd");
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.