gpt4 book ai didi

javascript - Bootstrap twitter 3.0 和 knockoutjs 验证

转载 作者:IT王子 更新时间:2023-10-29 03:21:30 26 4
gpt4 key购买 nike

使用 Twitter Bootstrap 验证类,例如has-errorhas-warning 需要放在包装 form-group 元素上,以便设置输入元素及其标签的样式。但是 Knockout-Validation 将类添加到输入元素。

<div class="form-group has-error">
<label class="control-label">Input with error</label>
<input type="text" class="form-control">
</div>

是否可以通过将类添加到 div 而不是 input 的方式来配置 Knockout-Validation?

最佳答案

thebringking 的回答不会让您一路走下去。您还需要指定 errorMessageClassdecorateInputElement 选项。

ko.validation.init({
errorElementClass: 'has-error',
errorMessageClass: 'help-block',
decorateInputElement: true
});

var viewModel = ko.validatedObservable({
name: ko.observable().extend({
required: true
}),
email: ko.observable().extend({
required: true,
email: true
}),
submit: function() {

if (!this.isValid()) {
this.errors.showAllMessages();
} else {
alert('good job');
}
}
});

ko.applyBindings(viewModel);
<link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.2/css/bootstrap.min.css" rel="stylesheet" />


<div class="container">
<form class="form-horizontal">
<div class="form-group" data-bind="validationElement: name">
<label class="control-label col-xs-2" for="name">Name</label>
<div class="col-xs-10">
<input id="name" class="form-control" type="text" data-bind="textInput: name" />
</div>
</div>
<div class="form-group" data-bind="validationElement: email">
<label class="control-label col-xs-2" for="email">Email</label>
<div class="col-xs-10">
<input id="email" class="form-control" type="text" data-bind="textInput: email" />
</div>
</div>
<div class="form-group">
<div class="col-xs-offset-2 col-xs-10">
<button type="submit" class="btn btn-primary" data-bind="click: submit">Submit</button>
</div>
</div>
</form>
</div>


<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout/3.3.0/knockout-min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/knockout-validation/2.0.3/knockout.validation.min.js"></script>

关于javascript - Bootstrap twitter 3.0 和 knockoutjs 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18402671/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com