敲除验证并显示工具提示

用户名

我有带有基因敲除验证的示例Web应用程序。我需要带提示信息的显示工具提示以显示必填属性-requiredText。我想在viewmodel中创建工具提示,而不是在html中创建,但是我不能吗?

HTML:

<!DOCTYPE html>
<html>
<head>
    <title>ko.validation.test</title>
    <link href="Content/bootstrap.css" rel="stylesheet" />
    <link href="Content/bootstrap-theme.css" rel="stylesheet" />
</head>
<body>
    <div class="container">
        <h2>Required field validation</h2>
        <div class="bs-docs-example">
            <form>
                <fieldset>
                    <div class="control-group" data-bind="validationElement: requiredText">
                        <label>Example 1 required</label>
                        <div class="input-append">
                            <input type="text" data-bind="value: requiredText" />
                            <button class="btn" data-bind="click: validateExample1Clicked">Validate</button>
                        </div>
                        <span class="label label-success" data-bind="visible: showExample1Success">Example 1 is valid</span>
                        <span class="label label-important" data-bind="visible: showExample1Failed">Example 1 is not valid</span>
                        <span class="help-inline" data-bind="validationMessage: requiredText"></span>
                    </div>
                    <div class="control-group" data-bind="validationElement: requiredTextUsingAttributes">
                        <label>Example 2 required attribute</label>
                        <div class="input-append">
                            <input type="text" data-bind="value: requiredTextUsingAttributes" required />
                            <button class="btn" data-bind="click: validateExample2Clicked">Validate</button>
                        </div>
                        <span class="label label-success" data-bind="visible: showExample2Success">Example 2 is valid</span>
                        <span class="label label-important" data-bind="visible: showExample2Failed">Example 2 is not valid</span>
                        <span class="help-inline" data-bind="validationMessage: requiredTextUsingAttributes"></span>
                    </div>
                    <div class="control-group">
                        <label>Optional</label>
                        <input type="text" data-bind="value: optionalText" />
                    </div>
                    <div class="form-actions">
                        <button class="btn btn-primary" data-bind="click: validateAllClicked">Validate all</button>
                    </div>
                    <div class="alert alert-error" data-bind="visible: showValidationErrors">
                        <strong>Not valid</strong> All the fields in the form are not valid.
                    </div>
                    <div class="alert alert-success" data-bind="visible: showValidationSuccess">
                        <strong>Valid</strong> All the fields are valid.
                    </div>
                    <div class="alert alert-info" data-bind="visible: errors().length > 0">
                        <strong>Form is not valid</strong> The form has following errors:
                        <ul data-bind="foreach: errors">
                            <li data-bind="text: $data"></li>
                        </ul>
                    </div>
                </fieldset>
            </form>
        </div>
    </div>
    <script src="Scripts/jquery-2.1.1.js"></script>
    <script src="Scripts/knockout-3.2.0.js"></script>
    <script src="Scripts/knockout.validation.js"></script>
    <script src="Scripts/bootstrap.js"></script>
    <script src="ts/TestViewModel.js"></script>
</body>
</html>

JavaScript:

/// <reference path="../scripts/typings/knockout/knockout.d.ts" />
/// <reference path="../scripts/typings/knockout.validation/knockout.validation.d.ts" />
/// <reference path="../scripts/typings/bootstrap/bootstrap.d.ts" />
var TestViewModel = (function () {
    function TestViewModel() {
        var _this = this;

        this.requiredText = ko.observable().extend({ required: true });

        this.optionalText = ko.observable();

        this.requiredTextUsingAttributes = ko.observable();

        this.errors = ko.validation.group(this);

        this.showValidationErrors = ko.observable(false);
        this.showValidationSuccess = ko.observable(false);
        this.showExample1Success = ko.observable(false);
        this.showExample2Success = ko.observable(false);
        this.showExample1Failed = ko.observable(false);
        this.showExample2Failed = ko.observable(false);

        this.validateExample1Clicked = function () {
            if (!ko.validation.validateObservable(_this.requiredText)) {
                alert("rrrrrr");
// Create tooltip 
        };

        this.validateExample2Clicked = function () {
            if (ko.validation.validateObservable(_this.requiredTextUsingAttributes)) {
                _this.showExample2Success(true);
                _this.showExample2Failed(false);
            } else {
                _this.showExample2Success(false);
                _this.showExample2Failed(true);
            }
        };

        this.validateAllClicked = function () {
            if (_this.errors().length == 0) {
                _this.showValidationSuccess(true);
                _this.showValidationErrors(false);
            } else {
                _this.showValidationSuccess(false);
                _this.showValidationErrors(true);
            }
        };
    }
    return TestViewModel;
})();


ko.validation.init({
    parseInputAttributes: true,

    decorateElement: true,

    errorElementClass: 'error'
});

ko.applyBindings(new TestViewModel());
//# sourceMappingURL=TestViewModel.js.map
zrabzdn
HTML:

<script src="Scripts/jquery-2.1.1.min.js"></script>
<script src="Scripts/knockout-3.2.0.js"></script>
<script src="Scripts/knockout.validation.js"></script>
<link href="Content/bootstrap.min.css" rel="stylesheet" />
<script src="Scripts/bootstrap.min.js"></script>
<script src="Scripts/ViewModel.js"></script>

<style>
    body {
    font-family: Helvetica, Arial, sans-serif;
    padding: 10px;
    }
    fieldset {
        padding: 10px;
        border: solid 1px #ccc;
        width: 500px
    }
    label {
        display: block;
        width: 100%;
        padding: 5px
    }
    .validationMessage {
        color: Red;
    }
    .Warning {
        border-color: red;
    }
    .customMessage {
        color: Orange;
    }
    input {
        border:1px solid #ccc
    }
</style>

<div id="contain" class="container">
    <h2>Required field validation</h2>
    <input data-bind="value: lastName, tooltip: isValidField" data-placement="below" data-title="Alert" data-content="We have identified this information is incorrect and must be updated." />
    <button type="button" data-bind='click: submit'>Submit</button>
</div>


JavaScript:

ko.validation.rules.pattern.message = 'Invalid.';
ko.validation.configure({
    //decorateInputElement: true,
    decorateElement: true,
    registerExtenders: true,
    messagesOnModified: false,
    insertMessages: false,
    parseInputAttributes: true,
    messageTemplate: null,
    errorClass: 'Warning'
    //      errorAsTitle: true
});

var ViewModel = function () {
    var self = this;
    self.lastName = ko.observable().extend({ required: true });
    self.isValidField = ko.observable();
    this.submit = function () {
        if (self.errors().length === 0) {
            alert('Thank you.');
        } else {
            self.errors.showAllMessages();
            self.isValidField(self.lastName.isValid());
        }
    };
    self.errors = ko.validation.group(self);
};

ko.bindingHandlers.popover = {
    init: function (element, valueAccessor, allBindingsAccessor) {
        var value = valueAccessor(),
            valueUnwrap = ko.unwrap(value),
            allBindings = allBindingsAccessor(),
            isValid = allBindings.value;
        if (isValid) {
            $(element).popover({
                trigger: "hover"
            });
        } else {
            $(element).popover("hide");
        }
    },

    update: function (element, valueAccessor, allBindingsAccessor) {
        var value = valueAccessor(),
            valueUnwrap = ko.unwrap(value),
            allBindings = allBindingsAccessor(),
            isValid = allBindings.value;
        if (isValid) {
            $(element).popover({
                trigger: "hover"
            });
        } else {
            $(element).popover("hide");
        }
    }
};

ko.bindingHandlers.tooltip = {
    init: function(element, valueAccessor) {
        var local = ko.utils.unwrapObservable(valueAccessor()),
            options = {};

        ko.utils.extend(options, ko.bindingHandlers.tooltip.options);
        ko.utils.extend(options, local);

        $(element).tooltip(options);

        ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            $(element).tooltip("destroy");
        });
    },
    options: {
        placement: "right",
        trigger: "hover"
    }
};

$(document).ready(function () {
    ko.applyBindings(new ViewModel());
});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

用于显示字段的工具提示和显示Jquery验证中的错误的工具提示

来自分类Dev

libgdx显示工具提示

来自分类Dev

工具提示未显示

来自分类Dev

显示在工具提示图标顶部的工具提示

来自分类Dev

敲除验证-如何显示单个错误消息

来自分类Dev

样式默认验证工具提示

来自分类Dev

同时显示工具提示提示和焦点

来自分类Dev

Bootstrap工具提示未显示

来自分类Dev

以编程方式显示工具提示

来自分类Dev

在悬停时显示工具提示

来自分类Dev

无法显示引导工具提示

来自分类Dev

使用JavaScript显示工具提示

来自分类Dev

工具提示引导未显示

来自分类Dev

AngularJS工具提示显示数据

来自分类Dev

无法显示引导工具提示

来自分类Dev

HTML工具提示显示属性

来自分类Dev

CMFCMenuBar 显示错误的工具提示

来自分类Dev

工具提示不显示结果

来自分类Dev

如何在jqueryui工具提示中显示jqueryui验证错误消息

来自分类Dev

Highcharts工具提示以毫秒为单位显示工具提示日期

来自分类Dev

显示工具提示时“实时”更新小部件的工具提示

来自分类Dev

使用Kendo UI验证程序如何隐藏默认验证错误消息并仅在工具提示上显示消息

来自分类Dev

如何使用敲除和工具提示在Array对象之间添加间隔

来自分类Dev

如何使用敲除和工具提示在Array对象之间添加间隔

来自分类Dev

GoogleCharts API-显示多个工具提示

来自分类Dev

WPF网格工具提示不显示

来自分类Dev

Highcharts同步图表显示工具提示

来自分类Dev

如何在页面底部显示工具提示

来自分类Dev

ChartJS:显示默认的工具提示onclick