简体   繁体   English

jQuery自动更正对重复字段的行为很奇怪

[英]jQuery auto correction acts weird on duplicated fields

I am using a script to auto-correct input on forms using jQuery. 我正在使用脚本来使用jQuery自动更正表单上的输入。 For example, when someone writes "abc" as his initials, the field will auto-correct the input directly to ABC 例如,当某人以“ abc”作为首字母缩写时,该字段将直接自动将输入更正为ABC

These scripts work excellent. 这些脚本工作出色。 However, anyone can fill out several forms with several names. 但是,任何人都可以用几种名称填写几种形式。 I am using knockout to duplicate the forms. 我正在使用淘汰赛来复制表格。 So far so good, but auto-correction doesn't work on duplicated fields anymore .. 到目前为止还算不错,但是自动校正不再适用于重复的字段 ..

The auto-correction looks like this (small part): 自动校正如下所示(一小部分):

// Lowercase
    $(".lowercase").keyup(function(e)
    {
        $(".lowercase").val(($(".lowercase").val()).toLowerCase());
        if (/[a-z]/g.test(this.value))
        {
            this.value = this.value.replace(/[^a-z ]/g, '');
        }

    });

    // Initials
    $(".initials").focus(function() {
        var current = $(".initials").val();
        $(".initials").keyup(function(e) {
            var key = String.fromCharCode(e.keyCode);
            if (key >= 'A' && key <= 'Z') {
                current += key + ".";
                this.value = current;
            }
            else {
                current = "";
            }
        });
    $(".initials").blur(function() {
        var i = $(".initials").val();
        var last = i[i.length - 1];
        if (last != "." && i.length !== 0){
            this.value += ".";
            }
        });
    });


    // Capitalize
    $(".cap").keyup(function(e)
    {
        function convertToUpper() {
        return arguments[0].toUpperCase();
         }
        val = this.value.toLowerCase().replace(/\b[a-z]/g, convertToUpper);
        this.value = val;

    });

A fiddle can be found here 这里可以找到一个小提琴

Update 更新资料

Thanks to raghaw Numbers now work. 感谢raghaw Numbers现在可以工作了。 But other fields don't yet . 但是其他领域还没有

You are binding event that is not working on elements that get created in future. 您正在绑定事件,该事件不适用于将来创建的元素。 Here is the change I made to your code: 这是我对您的代码所做的更改:

$(document).on("keyup", ".numbers", function(e)  
// $(".numbers").keyup(function(e)

Your modified fiddle is here http://jsfiddle.net/QUxyy/9/ 您修改过的小提琴在这里http://jsfiddle.net/QUxyy/9/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM