【问题标题】:Validating Radio Buttons with Knockout and Knockout Validation not applying expected CSS class使用 Knockout 和 Knockout Validation 验证单选按钮未应用预期的 CSS 类
【发布时间】:2012-12-02 20:41:26
【问题描述】:

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

我正在使用淘汰赛 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);

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

【问题讨论】:

  • 这个有 jsfiddle 或 jsbin 吗?

标签: javascript knockout.js knockout-validation


【解决方案1】:

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

【讨论】:

  • 我将此标记为答案。那个github问题/问题是我打开的:D :)
  • 好样的!很高兴答案是这样的
猜你喜欢
  • 2015-11-23
  • 1970-01-01
  • 2013-04-20
  • 2014-09-25
  • 2014-09-08
  • 2023-03-13
  • 2013-03-31
  • 2017-02-04
  • 1970-01-01
相关资源
最近更新 更多