Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 关于敲除绑定的JQuery_Javascript_Jquery_Html_Knockout.js - Fatal编程技术网

Javascript 关于敲除绑定的JQuery

Javascript 关于敲除绑定的JQuery,javascript,jquery,html,knockout.js,Javascript,Jquery,Html,Knockout.js,我有一张有两列的桌子,如下所示 问题:当我在文本框上尝试Jquery模糊事件时,它会在第一行工作,但当我向可观察数组添加新行时,会将新行显示出来。模糊事件对新行没有任何影响 我正在使用Knockout.js JQUERY HTML .blur仅绑定到现有行。使用事件委派 jQuery(document).on("blur", "#2CrpAcrs", function () { 顺便说一下,ID必须是唯一的,因此您不应该使用2CRPACR,而应该使用类。最好使用比文档更具体的选择器,例如要添加

我有一张有两列的桌子,如下所示

问题:当我在文本框上尝试Jquery模糊事件时,它会在第一行工作,但当我向可观察数组添加新行时,会将新行显示出来。模糊事件对新行没有任何影响

我正在使用Knockout.js

JQUERY

HTML

.blur仅绑定到现有行。使用事件委派

jQuery(document).on("blur", "#2CrpAcrs", function () {

顺便说一下,ID必须是唯一的,因此您不应该使用2CRPACR,而应该使用类。最好使用比文档更具体的选择器,例如要添加行的表上的类或ID。

编辑:请参见下面的“我的编辑”。我建议重新考虑你的代码

使用Knockout执行此类操作的方法是使用自定义绑定处理程序:

我想你会这样做:

ko.bindingHandlers.yourBindingName = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here
        $(element).blur(function () {
            var numb = $(this).val();
            numb = Math.round(numb * 100) / 100
            $(this).val(numb);
         });
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever any observables/computeds that are accessed change
        // Update the DOM element based on the supplied values here.
    }
};
<input id="2CrpAcrs" class="form-control" type="text" data-bind="value: NumAcres, yourBindingName: {}">
然后在您的元素中,您可以执行以下操作:

ko.bindingHandlers.yourBindingName = {
    init: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here
        $(element).blur(function () {
            var numb = $(this).val();
            numb = Math.round(numb * 100) / 100
            $(this).val(numb);
         });
    },
    update: function(element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever any observables/computeds that are accessed change
        // Update the DOM element based on the supplied values here.
    }
};
<input id="2CrpAcrs" class="form-control" type="text" data-bind="value: NumAcres, yourBindingName: {}">
此外,当前代码会导致多个元素具有相同的ID,这是无效的。考虑使用类代替2CRPAR的ID。这可能是您当前问题的一部分

编辑:


为什么要使用jQuery?为什么不直接订阅你的价值观呢?这就是淘汰赛的目的。您可以订阅数据绑定到输入元素的任何内容,并对需要的值执行任何自定义处理。

因此,如果我将say id=2CrpAcrsTable添加到此表中,并以jQuery2CrpAcrsTable.onblur、2CrpAcrs、function{}的形式运行函数如何为从knockout添加的每个新行获取唯一IDarray@sss111行不需要ID。使用class.2CrpAcrs之类的工具。在blur方法中,将当前模糊输入称为$this我有点困惑,我已经为元素创建了一个类,请您重做代码,以便我知道在哪里使用当前模糊元素看看我在代码中做了什么。我不使用任何身份证。如果不使用我建议的bindingHandler,您也可以通过使用jQuerydocument.onblur、2CrpAcrs和函数来完成同样的事情{as@ExplosionPills suggestscan请将我指向soem链接,我对使用class而不是ID感到困惑,因为我确实为该元素提供了一个类。每个元素上都可以有多个类。如果您按照我的建议使用自定义绑定处理程序,您根本不需要ID。因此,请将其删除。您不能有多个元素如果ID相同,则无效。