jquery 验证插件。生成随机成功消息一次
昨天我发布了这个问题,该问题已得到解答,但它导致了另一个问题。
我使用 jquery 验证插件有以下代码:
//array of success messages and randomly select one
var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!","Yay!", "Success!", "Ok!", "Perfect!","Sweet!" ];
function successMessage(label) {
return messages[Math.floor(Math.random() * messages.length)];
}
然后我的验证代码成功
...success: function(label) {
if(!$(label).hasClass("valid")){
label.addClass("valid").text(successMessage());
}
}...
发生的情况是,每次表单有效(在 keyup 或 focus 上)时,它都会重新生成一条成功消息。我想,既然我添加了“有效”类,一个逻辑步骤就是检查标签是否具有“有效”类,因此不要添加消息,因为它已经有一个消息。然而这不起作用。任何其他想法都会很棒。
编辑:经过进一步的研究和思考。我很确定问题是每次验证表单时(在 keyup、提交等)时都会删除并添加“valid”类,我认为最简单的事情可能是选择标签的值并查看结果是否与数组匹配,如果是,则不执行任何其他操作,添加成功消息。我可能是错的。我也不认为我第一次就很好地阐明了这个问题。在用户打字时更改验证消息看起来很愚蠢。
编辑2在提出许多澄清建议和示例代码之后,我发布此链接http: //jsfiddle.net/Ye3Ls/20/ 如果您在字段中键入,直到收到成功消息,然后继续键入,您就会看到问题所在。我正在使用此处找到的验证插件感谢所有患者我整理了如何让它发挥作用。我想我可能需要使用像 in_array 或 has_value() 这样的东西
Yesterday I posted this question which is answered but it lead to another problem.
I have the following code using jquery validation plugin:
//array of success messages and randomly select one
var messages = ["Good!", "Great!", "Awesome!", "Super!", "Nice!","Yay!", "Success!", "Ok!", "Perfect!","Sweet!" ];
function successMessage(label) {
return messages[Math.floor(Math.random() * messages.length)];
}
Then my validation code success
...success: function(label) {
if(!$(label).hasClass("valid")){
label.addClass("valid").text(successMessage());
}
}...
What is happening is that each time the form is being valid (on keyup or foucus) it regenerates a success message. I figured since i was adding the class "valid" a logical step would be to check if the label has the "valid" class and is so don't add a message because it already has one. This however isn't working. Any other ideas would be great.
Edit: So after further research and thinking. I'm pretty sure the problem is that the class of "valid" is being removed and added each time time form is validated (on keyup, submit, etc) I think the easiest thing to do may be to select the value of the label and look to see if the result matches the array, if so then don't do anything else add a success message. I could be wrong. I also don't think I articulated the question the best the first time. It just looks silly to have the validation message change while the user is typing.
EDIT 2 After the many suggestions for clarification and example code I am posting this link http://jsfiddle.net/Ye3Ls/20/ if you type into a field until you get a success message then keep typing you'll see what the problem is. I'm using the validation plugin found here Thanks for all your patients as I sort though how to get this to work. I think i may need to use something like in_array or has_value()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不需要 label 作为
successMessage
的参数。我相信如果 label 已经是一个 jQuery 对象,执行此操作:
$(label)
将创建一个附加到 jQuery 的新标签 jQuery 对象。试试这个:或者
甚至更好:
[编辑]在评论中提出问题后
我假设您需要将
valid
类添加到label
。如果不这样做,请删除else
语句。我还假设文本出现在
label
中的唯一方法是通过successMessage
。因此,您不需要向标签
添加任何文本。它将保持不变。附带说明一下,如果插件正在更改 HTML 元素的类,那么这将是代码的严重副作用,我个人不会容忍这种情况。
现在可能发生的更合乎逻辑的事情是您在提交后重新加载内容。如果这是真的,那么您在提交之前对 DOM 所做的所有 jQuery 操作都将丢失,因为新内容将被重新加载。
在将来添加实际插件的链接和更完整的代码来使用将非常重要。 @Nick Craver 使用 jsfiddle.net,我使用 jsbin.com 发布示例代码。这可以是我们所有部分更加协作的努力,以便能够重现和解决问题。
[编辑1A]
这里
问题是标签被创建了多次。我认为这个插件不太好用。
我必须改变错误位置和成功逻辑。
Don't need label as a parameter for
successMessage
.I believe that if label is already a jQuery object doing this:
$(label)
will create a new label jQuery object attached to jQuery. Try this:Or
Or even better:
[EDIT] after question asked in comment
I'm assuming that you need to add the
valid
class tolabel
. If you don't then remove theelse
statement.I'm also assuming that the only way for text to be in
label
is throughsuccessMessage
. Therefore, you will not need to add any text tolabel
. It will stay the same.On a side note, if the plug-in is changing the classes of your HTML elements then that is a serious side-effect of the code and I personally wouldn't put up with that.
Now the more logical thing that is probably happening is that you are reloading your content after doing your submission. If that is true then all the jQuery manipulations to the DOM you did before submission will be lost, because of new content be reloaded.
It would be really important in the future to add a link to the actual plug-in and more complete code to work with. @Nick Craver uses jsfiddle.net and I use jsbin.com to post sample code. This can be a more collaborative effort on all our parts to be able to reproduce and solve the problem.
[EDIT 1A]
Here it is
The problem is that the label is being created more than once. This plug-in in my opinion is not so easy to work with.
I had to change the error placement and success logic.
看起来你的意思是:
而不是:
It looks like you mean to say:
rather than:
如果 label 是一个变量,并且 label.addClass("valid") 工作正常,为什么不使用
if(!((label).hasClass("valid"))){
而 不是进行验证
if(!$(标签).hasClass("有效")){
If label is a variable, and label.addClass("valid") works fine, why don't you verify using:
if(!((label).hasClass("valid"))){
instead of
if(!$(label).hasClass("valid")){