因此,基本上我正在尝试做的安全措施(和学习过程)是我自己的"Capthca"系统.发生的事情是我有二十个"标签"(为简洁起见,下面只显示一个),每个都有1到20之间的ID.我的javascript随机选择其中一个ID并使该图片显示为安全代码.每个标签都有自己的值,对应于验证码图像的文本.
另外,我最初禁用了提交按钮.
我需要帮助的是在有人输入与HTML标签元素中列出的值匹配的正确值时,弄清楚如何启用提交按钮.
我已经发布了用户输入值和ID的值,即使它们匹配,javascript也不会启用提交按钮.
我觉得这是一个非常简单的添加/修复.帮助将非常感谢!!!
HTML代码
Javascript代码
//Picks random image function pictureSelector() { var number = (Math.round(Math.random() * 20)); //Prevents zero from being randomly selected which would return an error if (number === 0) { number = 1; }; console.log(number); //Set the ID variable to select which image gets enabled pictureID = ("#" + number); //If the siblings have a class of enabled, remove it $(pictureID).siblings().removeClass("enabled"); //Add the disabled class to all of the sibling elements so that just the selected ID image is showing $(pictureID).siblings().addClass("disabled"); //Remove the disabled class from the selected ID $(pictureID).removeClass("disabled"); //Add the enabled class to the selected ID $(pictureID).addClass("enabled"); }; //Calls the pictureSelector function pictureSelector(); //Gets the value of the picture value var pictureValue = $(pictureID).attr("value"); console.log(pictureValue); //Gets the value of the security input box as the user presses the keys and stores it as the variable inputValue $("#contact-div-captcha-input input").keyup(function(){ var inputValue = $("#contact-div-captcha-input input").val(); console.log(inputValue); }); console.log($("#contact-div-captcha-input input").val()); //Checks to see if the two values match function equalCheck() { //If they match, remove the disabled attribute from the submit button if ($(pictureValue) == $("#contact-div-captcha-input input").val()) { $("#submit").removeAttr("disabled"); } }; equalCheck();
UPDATE
在这里小提琴
更新#2
$("#contact-div-captcha-input input").keyup(function(){ var inputValue = $("#contact-div-captcha-input input").val(); console.log(inputValue); if (pictureValue === inputValue) { $("#inputsubmit").removeAttr("disabled"); } });
所以我得到了99.9%的工作,现在唯一的问题是,如果有人要退格或删除他们输入的正确值,则提交按钮不会再变回禁用状态.有什么指针吗?
已知问题.
给你的按钮起一个名字submit
.那个名字干扰了表格submit
.
编辑
为此请求了一个链接 - 我没有纯JavaScript的链接,但jQuery文档确实提到了这个问题:
http://api.jquery.com/submit/
形式及其子元素不应该使用输入名称或ID与表格的属性,如冲突
submit
,length
或method
.名称冲突可能导致混乱的故障.有关规则的完整列表以及检查这些问题的标记,请参阅DOMLint.
编辑2
http://jsfiddle.net/m55asd0v/
你有CSS
和JavaScript
部分相反.该代码从未在JSFiddle中运行.
你永远不会再打电话equalCheck
.我给你的keyUp
处理程序添加了一个调用.
出于某种原因,你包装pictureValue
在一个jQuery对象中,因为$(pictureValue)
它无法完成你想要的任务.
基本调试101:
你内部的console.log equalCheck
会向你显示该函数只被调用一次.
检查您正在比较的值的控制台日志会显示您的值不正确.
对JSFiddle内部奇怪的突出显示的基本关注会显示你有错误类别的代码部分.