简体   繁体   English

如何根据页面中其他位置的文本停止某些输入值?

[英]How do I stop certain values of input, based on text elsewhere in the page?

I have an existing script which is running perfectly! 我有一个完美运行的现有脚本! It is coded for Firefox 2.x and Greasemonkey 0.8.x and I can't use jQuery! 它是针对Firefox 2.xGreasemonkey 0.8.x编码的,我不能使用jQuery!

OLD WORKING SCRIPT: 老工作脚本:

//--- Make sure this list of names is all uppercase.
var usersWhoCanSetPriority = ['JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY'];

var bDisablePrio    = true;
var tdNodes         = document.getElementsByTagName ("TD");
for (var J = tdNodes.length - 1;  J >= 0;  --J) {
    var tdNode      = tdNodes[J];
    if (tdNode.className == "user") {
        var userName        = tdNode.textContent.replace (
            /^(?:.|\n|\r)+\(User:\s+([^)]+)\)(?:.|\n|\r)+$/i, "$1"
        ).toUpperCase ();

        if (usersWhoCanSetPriority.indexOf (userName) > -1) {
            bDisablePrio    = false;
        }
    }
}

if (bDisablePrio) {
    var oldInput = document.getElementsByName ("prio");
    oldInput[0].setAttribute ("disabled", "disabled");
}


  1. Here's a fiddle of how it looks/acts when the user is not in usersWhoCanSetPriority . 这是当用户不在 usersWhoCanSetPriority时它的外观/动作的小提琴

  2. Here's a fiddle of how it looks/acts when the user has full access . 这是用户具有完全访问权限时其外观/行为的小提琴


NEW SCRIPT EXAMPLE: 新脚本示例:

Now, if the destination input is present, privileged users should be able to set any value. 现在,如果存在目标输入,则特权用户应该能够设置任何值。
But unprivileged users should be blocked from setting 12 and 22 . 但是应该阻止非特权用户设置1222

  1. Privileged user, with Destination field . 特权用户,带有“目标”字段

  2. Restricted user, with Destination field . 受限制的用户,带有目标字段

That is, if the user is NOT -> 'JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY' (from footer) then disable the prio input, and stop the input when user is entering "12" or "22", and write an error message. 也就是说,如果用户不是 - >'JOHN','LUKE','JEFF','MAX','ANDY'(来自页脚)然后禁用prio输入,并在用户输入“12”时停止输入或“22”,并写入错误消息。

You have to know that Steve ("not-allowed user") is allowed to enter for example 120, 121, 220, 222 and all other numbers but not the 12 and 22 ! 您必须知道允许史蒂夫(“不允许的用户”)输入例如120,121,220,222和所有其他数字,但不能输入1222 I don't know if it is a problem because the 12 is also in 120! 我不知道这是不是一个问题,因为12也是120! 'JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY' are allowed to enter all numbers into the input. 'JOHN','LUKE','JEFF','MAX','ANDY'被允许输入所有数字。

The old script runs on different pages like site-one.html and site-two.html However the destination input is on site-one.html and on site-three.html - it means sometimes I have only the prio-input and sometimes I have both the prio-input and destination-input. 旧脚本运行在不同的页面上,例如site-one.html和site-two.html但是目标输入位于site-one.html和site-three.html上 - 这意味着有时我只有prio-input,有时候我有prio输入和目的地输入。

How do I do all that? 我该怎么做?

Regards, Bernte 此致,Bernte

See http://jsfiddle.net/WT9yZ/4/ http://jsfiddle.net/WT9yZ/4/

You can add 你可以加

if(bDisablePrio){
    var destExcluded=['12','22'];
    document.getElementById('dest').onchange=function(){
        if(destExcluded.indexOf(this.value)>-1){
            this.value='';
        }
    }
}

The problem is that if "122" is allowed but "12" isn't, you can't use onkeypress event in order to prevent that the user writes that, because if the user can't write "12" , he can't write "122" either. 问题是,如果允许"122"但不允许"12" ,则不能使用onkeypress事件以防止用户写入,因为如果用户不能写"12" ,他就可以'写"122"

Then, the solution is the onchange event, which is fired when the textfield loses focus (if it has been modified). 然后,解决方案是onchange事件,当文本字段失去焦点(如果它已被修改)时触发。 Then, if the user has written only "12" or "22" , you can clear the textfield. 然后,如果用户只写了"12""22" ,则可以清除文本字段。

User user1653020's answer is close, but it has several problems: 用户user1653020的答案很接近,但它有几个问题:

  1. It will fail in Greasemonkey 0.8! 它会在Greasemonkey 0.8中失败! See "Pitfall #2: Event Handlers" . 请参阅“陷阱#2:事件处理程序”
  2. It will crash when destination is not present. destination不存在时会崩溃。
  3. It is not very user-friendly. 它不是非常用户友好。 (Doesn't let the user know what she is doing wrong.) (不要让用户知道她做错了什么。)
  4. Allows for the user to submit invalid values. 允许用户提交无效值。

Go to jsfiddle.net/P2VeG , enter 12 for the destination and press Enter or click the "Submit" button. 转到jsfiddle.net/P2VeG ,输入12作为目的地,然后按Enter键或单击“提交”按钮。 You'll see that the form gets sent to the server, and with an invalid Destination value and no warning to the user! 您将看到表单被发送到服务器,并且具有无效的Destination值并且没有警告用户!

The following code should overcome all of those problems. 以下代码应该克服所有这些问题。 (But it's untested in GM 0.8 and FF2, for obvious reasons.) (但由于显而易见的原因,它在GM 0.8和FF2中未经测试。)

See the code in action at jsfiddle.net/P2VeG/2 . 请参阅jsfiddle.net/P2VeG/2上的代码

//--- Make sure this list of names is all uppercase.
var usersWhoCanSetPriority  = ['JOHN', 'LUKE', 'JEFF', 'MAX', 'ANDY'];
var excludedDestinations    = ['12', '22'];

var bDisablePrio    = true;
var tdNodes         = document.getElementsByTagName ("TD");
for (var J = tdNodes.length - 1;  J >= 0;  --J) {
    var tdNode      = tdNodes[J];
    if (tdNode.className == "user") {
        var userName        = tdNode.textContent.replace (
            /^(?:.|\n|\r)+\(User:\s+([^)]+)\)(?:.|\n|\r)+$/i, "$1"
        ).toUpperCase ();
        if (usersWhoCanSetPriority.indexOf (userName) > -1) {
            bDisablePrio = false;
        }
    }
}

if (bDisablePrio) {
    var oldInput    = document.getElementsByName ("prio");
    if (oldInput  &&  oldInput.length) {
        oldInput[0].setAttribute ("disabled", "disabled");
    }

    var destInput   = document.getElementsByName ("dest");
    if (destInput  &&  destInput.length) {
        destInput[0].addEventListener (
            "change",
            function (zEvent) {
                bCheckdestinationValue (destInput[0]);
            },
            false
        );

        //--- Extra added to try and get FF2 to behave as later versions do.
        destInput[0].addEventListener (
            "keydown",
            function (zEvent) {
                if (zEvent.keyCode == 13) {
                    if ( bCheckdestinationValue (destInput[0]) ) {
                        zEvent.preventDefault ();
                        zEvent.stopPropagation ();
                        return false;
                    }
                }
            },
            true
        );

        destInput[0].form.addEventListener (
            "submit",
            function (zEvent) {
                var destValue   = destInput[0].value;
                if (    destValue === ''
                        ||  excludedDestinations.indexOf (destInput[0].value) > -1
                ) {
                    //--- Stop the submit
                    /*--- According to:
                        http://www.webdevout.net/browser-support-dom#dom2events
                        preventDefault() and stopPropagation() are supported by FF2
                    */
                    zEvent.preventDefault ();
                    zEvent.stopPropagation ();
                    return false;
                }
            },
            true
        );
    }
}

function bCheckdestinationValue (destInputNd) {
    //--- Returns true if value is bad.
    if (excludedDestinations.indexOf (destInputNd.value) > -1) {
        destInputNd.value = ''; // Blank input

        //--- Add or show Error message.
        var destErrNode = document.getElementById ("gmDestErrorDisp");
        if (destErrNode) {
            destErrNode.style.display = "inline";
        }
        else {
            destErrNode             = document.createElement ('b');
            destErrNode.id          = "gmDestErrorDisp";
            destErrNode.style.color = "red";
            destErrNode.textContent = "12 and 22 are forbidden";
            destInputNd.parentNode.appendChild (destErrNode);
        }
        //--- Uncomment this alert(), if all else fails in FF2.
        ////alert ('Destination cannot be 12 or 22.');
        return true;
    }
    else {
        var destErrNode = document.getElementById ("gmDestErrorDisp");
        if (destErrNode) {
            destErrNode.style.display = "none";
        }
    }
    return false;
}

暂无
暂无

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

相关问题 如何在页面上抓取某些文本? - How do I grab certain text on a page? 如果对象到达页面上的某个点,如何使对象停止? - How do I make an object stop if it reaches a certain point on a page? 如何在页面加载时停止执行某个javascript - How do I stop the execution of a certain javascript on page load 如何使用JavaScript将文本输入形式限制为一定范围的值? - How do I limit a text input form to a certain range of values with JavaScript? 页面加载后,如何将值加载到输入文本字段中? - How do I load values into input text fields once the page is loaded? 在输入中输入数据,然后仅显示在页面上其他位置输入的文本 - Entering data in an input and then displaying just the text entered elsewhere on page 输入框到达一定距离(以像素为单位)后,如何停止扩展输入框的宽度? - How do I stop expanding the width of the input box after it reaches a certain distance (measured in pixels)? 当我输入并提交内容时,如何停止输入表单以更改背景颜色和文本颜色? - How do I stop the input form to change background color and text color when I input and submit something? 如何阻止 firestore web 客户端根据文档内容阅读规则中的某些文档? - How do I stop a firestore web client from reading certain documents in rules based off a documents content? 我如何制作一个使用特定输入重定向到特定 html 页面的 js 代码? - How do i make a js code that redirects to a certain html page with a certain input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM