gpt4 book ai didi

javascript - 使用 Knockout 和 Knockout Validation 验证单选按钮未应用预期的 CSS 类

转载 作者:行者123 更新时间:2023-11-29 22:18:14 24 4
gpt4 key购买 nike

我有一个表单,其中有两组单选按钮。在每个集合中,用户必须选择一个(我们不会默认选择一个,以便用户必须“考虑”选择一个或另一个)。

我正在使用 knockout 2.2.0 和最新版本的 Knockout Validation .我遇到了单选按钮的问题,因为验证将正确确定未选择单选按钮,但是,它不会为单选按钮提供 css 类。如何获得将 css 应用于单选按钮输入的验证?

理想情况下,在确定未选择单选输入后,我想将“invalidElement”的 css 类应用于组中的两个单选按钮。

*注意:它将“invalidElement”的 css 类应用于所有其他输入,单选按钮输入除外。此外,我还考虑编写一些自定义的东西来突出显示未选中的单选按钮输入,但我不太熟悉 Knockout 来执行此操作。

(function ($) {
var viewModelSlide1;

$(function () { // Document ready
ko.validation.configure({
decorateElement: true, // Indicates whether to assign an error class to the <input> tag when your property is invalid
errorElementClass: 'invalidElement', // The CSS class assigned to validation error messages
registerExtenders: true,
messagesOnModified: true, // Indicates whether validation messages are triggered only when properties are modified or at all times
insertMessages: false, // Indicates whether <span> tags with the validation message will be inserted to the right of your <input> element
parseInputAttributes: true // Indicates whether to assign validation rules to your ViewModel using HTML5 validation attributes
});

function viewModelSlide1Definition() {
var self = this;

self.loanPurpose = ko.observable("").extend({ required: true });
self.hasAccount = ko.observable("").extend({ required: true });

//Validation observable(s)
self.errors = ko.validation.group(self);

submit: function () {
if (viewModelSlide1.errors().length == 0) {
alert('Thank you.');
} else {
alert('Please check your submission.');
viewModel.errors.showAllMessages(); // This will apply the css styles to all invalid inputs except radio buttons
}
}

};

// Activate knockout.js
viewModelSlide1 = new viewModelSlide1Definition();

ko.applyBindings(viewModelSlide1, $("#step-step-0")[0]);

}); // End document ready
})(jQuery);

带有绑定(bind)的 HTML...

  <div id="step">
<fieldset id="custom-step-1" class="step" title="Getting Started">
<label>Purchase</label><input class="required" type="radio" name="radioLoanPurpose" value="purchase" data-bind="checked: loanPurpose" />
<label>Refinance</label><input class="required" type="radio" name="radioLoanPurpose" value="refinance" data-bind="checked: loanPurpose" />
<br />
<label>Do you have an account</label>
<span>Yes</span><input type="radio" name="radioHaveAccount" value="true" data-bind="checked: hasAccount" />
<span>No</span><input type="radio" name="radioHaveAccount" value="false" data-bind="checked: hasAccount" />
</fieldset>
<input type="submit" class="finish" data-bind="click:submit"/>
</div>

最佳答案

我不确定这是 KO 还是 KO 验证的问题,但是使用 checked 而不是 value 的绑定(bind)无法显示验证器,如 http://jsfiddle.net/uXzEg/1/ 所示。

这是有效的 javascript:

$(function () { // Document ready


var viewModelSlide1Definition = ko.validatedObservable({
loanPurpose : ko.observable().extend({
required: true
}),
hasAccount: ko.observable().extend({
required: true
}),

submit: function () {
if (this.isValid()) {
alert('Thank you.');
} else {
console.log(this.hasAccount());
alert('Please check your submission.');
this.errors.showAllMessages();
}
}

});

// Activate knockout.js


ko.applyBindings(viewModelSlide1Definition);

}); // End document ready

和html

<div id="step">
<fieldset id="custom-step-1" class="step" title="Getting Started">
<label>Purchase</label>
<input type="radio" name="radioLoanPurpose"
value="purchase" data-bind="value: loanPurpose" />
<label>Refinance</label>
<input type="radio" name="radioLoanPurpose"
value="refinance" data-bind="value: loanPurpose" />
<br />
<label>Do you have an account</label> <span>Yes</span>

<input type="radio" name="radioHaveAccount"
value="true" data-bind="value: hasAccount" /> <span>No</span>

<input type="radio" name="radioHaveAccount" value="false"
data-bind="value: hasAccount" />
</fieldset>
<input type="submit" class="finish" data-bind="click:submit" />
</div>

以及导致我这样做的问题

https://github.com/ericmbarnard/Knockout-Validation/issues/193

关于javascript - 使用 Knockout 和 Knockout Validation 验证单选按钮未应用预期的 CSS 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13885584/

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