【问题标题】:Knockout 3.0- Uncaught Error: You cannot apply bindings multiple times to the same element.淘汰赛 3.0 - 未捕获的错误:您不能将绑定多次应用于同一元素。
【发布时间】:2014-10-21 17:51:09
【问题描述】:

我有一个包含多个部分的页面,这些部分一个接一个地加载(在第 1 部分验证时加载第二部分)。我正在使用淘汰赛 3.0 在每个部分中执行验证,但我遇到了错误- 未捕获的错误:您不能将绑定多次应用于同一元素。

请帮忙。谢谢!

HTML: 关闭 继续

<section id="modal2resetpwd" class="modal fade hide component">
<header class="modal-header">
    <h1 class="modal-title form-title" id="modalTitle"></h1>
</header>
<article class="modal-body" id="modalContent">
    <p class="intro-line">enter the id you want to use for verification</p>
    <table class="table">
        <tr>
            <td>
                <label>
                    <input type="radio" value="employeeid" name="identifier" checked="checked">employee id</label></td>
        </tr>
        <tr>
            <td>
                <label>
                    <input type="radio" value="buid" name="identifier">BU id</label></td>
        </tr>
    </table>
    <div data-bind='validationOptions: { messageTemplate: "customMessageTemplate" }'>
        <input style="width: 50%; margin-left: 20%;" data-bind="value: identifierId, valueUpdate: 'afterkeydown', event: { 'keyup': checkNumber }" />
    </div>
    <table class="table">
        <tr>
            <td>
                <label>
                    <input type="radio" value="email" name="identifier" disabled="disabled">email id</label></td>
        </tr>
        <tr>
            <td>
                <label>
                    <input type="radio" value="phone" name="identifier" disabled="disabled">phone number</label></td>
        </tr>
    </table>
</article>
<footer class="modal-footer">
    <div class="modal-controls">
        <a class="btn btn-icon" href="javascript:ActivatePanel(1)">
            <img class="icon-image" src="~/Content/Images/icon-return.png" />return</a>
        <a class="btn btn-icon" data-bind="click: ValidateIdentifierId">
            <img class="icon-image" src="~/Content/Images/icon-run.png" />proceed</a>
    </div>
</footer>

JS: var 模式 = { 电子邮件:/^([\d\w-.]+@([\d\w-]+.)+[\w]{2,4})?$/, 电话:/^\d[\d -]\d$/, 标识符:/^\d/, 邮政编码:/^([a-zA-Z]{1,2}[0-9][0-9]?[a-zA-Z\s]?\s*[0-9][a-zA -Z]{2,2})|(GIR 0AA)$/ };

var viewModelEmail = {
emailAddress: ko.observable().extend({
    required: { message: requiredEmail },
    pattern: {
        params: patterns.email,
        message: invalidEmail
    }
}),

ValidateEmailAddress: function () {
    if (viewModelEmail.emailerrors().length == 0) {
        // TO DO Validate entered email address 
        ActivatePanel(2);
    } else {
        viewModelEmail.emailerrors.showAllMessages();
    }
}
};

viewModelEmail.emailerrors = ko.validation.group(viewModelEmail);

ko.applyBindings(viewModelEmail, $("#modal1resetpwd")[0]);

var viewModelId = {
identifierId: ko.observable().extend({
    required: { message: requiredIdentifierId },
    minLength: 2,
    maxLength: 6,
    pattern: {
        params: patterns.identifier,
        message: invalidId
    }
}),

ValidateIdentifierId: function () {
    if (viewModelId.iderrors().length == 0) {
        // TO DO Validate entered email address 
        ActivatePanel(3);
    } else {
        viewModelId.iderrors.showAllMessages();
    }
}
};

viewModelId.iderrors = ko.validation.group(viewModelId);

ko.applyBindings(viewModelId, $("#modal2resetpwd")[0]);

【问题讨论】:

    标签: jquery knockout.js


    【解决方案1】:

    就像它说的那样,即使绑定不同,您也会为 #modal2resetpwd 调用两次 ko.applyBindings。您只能应用一次绑定。

    您可以将两个单独的视图模型包装在另一个对象中,即 实际的视图模型:

    ko.applyBindings({
        id: viewModelId,
        email: viewModelEmail,
    }, document.getElementById("modal2resetpwd"));
    

    那么您必须更新您的 HTML 才能使用它。

    data-bind="click: id.ValidateIdentifierId"
    

    【讨论】:

    • 感谢您的回复。我调用 ko.applyBindings 两次,一次用于 modal1resetpwd,然后用于 modal2resetpwd。它应该不起作用吗?
    猜你喜欢
    • 2016-04-29
    • 2015-01-06
    • 2016-10-13
    • 1970-01-01
    • 2014-04-13
    • 2016-09-26
    • 2017-05-05
    • 2013-07-16
    • 2014-01-22
    相关资源
    最近更新 更多