Javascript 将插入符号保留在第8个字符的代码段是;“始终”;1个字符

Javascript 将插入符号保留在第8个字符的代码段是;“始终”;1个字符,javascript,jquery,html,caret,Javascript,Jquery,Html,Caret,我使用此函数是为了防止插入符号进入前8个字符https://。这个想法是允许用户输入一个YouTube URL,因为所有YouTube.com URL都是https,我确保它始终存在,到目前为止,我已经这样做了,所以没有任何字符可以删除。 以下是我到目前为止的情况: $( "#main-entry-box" ).val("").focus().val("https://").on("keydown", function() { // This keydown is the releva

我使用此函数是为了防止插入符号进入前8个字符
https://
。这个想法是允许用户输入一个YouTube URL,因为所有YouTube.com URL都是https,我确保它始终存在,到目前为止,我已经这样做了,所以没有任何字符可以删除。 以下是我到目前为止的情况:

$( "#main-entry-box" ).val("").focus().val("https://").on("keydown", function() {

    // This keydown is the relevant code for the question, 
    // The rest was included in case it was interfering with this.

        if (this.selectionStart == this.selectionEnd) {
            if (this.selectionStart < 9) {
                this.selectionStart = 9;
                this.selectionEnd = 9;
            }
        }
    }).on("keyup propertychange input paste", function() {

        if (this.value == "https:/") {
            this.value = "https://";
        }

        else if (this.value.substring(0,8) != "https://") {
            this.value = "https://" + this.value.substring(7).replace(/^https?:\/\//gi, "");
        }

        else {
            this.value = "https://" + this.value.substring(8).replace(/^https?:\/\//g, "");
        }
    });
$(“#主输入框”).val(“”.focus().val(“https:/”).on(“keydown”,function()){
//此按键是问题的相关代码,
//其余的都包括在内,以防它干扰这一点。
if(this.selectionStart==this.selectionEnd){
如果(此.selectionStart<9){
this.selectionStart=9;
this.selectionEnd=9;
}
}
}).on(“键控属性更改输入粘贴”,函数(){
如果(this.value==“https:/”){
this.value=“https://”;
}
else if(此.value.substring(0,8)!=“https://”){
this.value=“https://”+this.value.substring(7).替换(//^https?:\//\///gi,”);
}
否则{
this.value=“https://”+this.value.substring(8).替换(/^https?:\/\//g,”);
}
});
#主输入框
是相关输入

这样做的目的是将插入符号保留在
https://
字符之后,但目前它只保留在第一个
/
字符之后


这也是一个难题。

解决另一个问题似乎是一个合理的方法。“查看此难题的代码。”-不,请将代码包含在问题中,否则它可能会被关闭为离题。@cwk我已经看过类似的内容,我正在考虑它。但我真的无法理解我在这方面做错了什么,就我所能看到的而言,这应该是可行的。。。如果我无法找到该代码段的解决方案,我将研究其他解决方案。@David Thomas会的,我现在就编辑它。如果它总是少1个字符,为什么不直接添加或减除1以到达您需要的任何位置?