3

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

我正在使用淘汰赛 2.2.0 和最新版本的淘汰赛验证。我遇到了单选按钮的问题,因为验证将正确确定未选择单选按钮,但是,它没有为单选按钮提供 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);

带有绑定的 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>
4

1 回答 1

2

我不确定这是否是 KO 或 KO 验证的问题,但使用检查而不是值的绑定无法显示验证器,如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

于 2013-02-01T15:58:08.233 回答