简体   繁体   English

自动校正对重复的字段起作用

[英]Autocorrection acts weird on duplicated fields

I have a form that users can duplicate, so they can submit multiple forms in one times. 我有一个用户可以复制的表格,因此他们可以一次提交多个表格。 This works fine. 这很好。 However, i noticed that users don't use it the right way. 但是,我注意到用户没有正确使用它。 For example, i ask for initials and people fill out their front name. 例如,我要求输入缩写,然后人们填写其姓氏。 PHP checks all fields and auto-corrects them if necessary, but this turns John into JOHN PHP检查所有字段并在必要时自动更正它们,但这将John变成JOHN

Anyway a good way would be to autocorrectfields. 无论如何,一种好方法是自动更正字段。 This works on the initial form, but goes wrong on the duplicated forms. 这适用于初始表单,但是在重复的表单上出错。 Especially initials acts weird. 尤其是姓名缩写的行为很奇怪。 My knowledge of jQuery and Javascript is very limited, so after puzzling for weeks i still don't know how to solve it. 我对jQuery和Javascript的了解非常有限,因此经过数周的困惑后,我仍然不知道如何解决它。 Suggestions are welcome. 欢迎提出建议。

Fiddle: http://jsfiddle.net/QUxyy/15/ 小提琴: http : //jsfiddle.net/QUxyy/15/

JS JS

// 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;

    });

Click for full code and preview 点击查看完整代码并预览

In your callback event handlers function for .voorletters use 'this', eg: 在您的.voorletters的回调事件处理函数中,使用'this',例如:

SEE DEMO 查看演示

$(document).on("keydown", ".voorletters", function(e)  {
            var i = $(this).val();
            if (e.keyCode == 8 && i.charAt(i.length - 1) == ".") {
                $(this).val(i.slice(0, -1));
                current = $(this).val();
            }
        })

And you should do it for all your call back function as .initials, .lowercase, etc... 您应该为所有的回调函数使用.initials,.lowercase等。

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

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