文本输入值大于.. jQuery [英] text input value greater than.. jQuery

查看:65
本文介绍了文本输入值大于.. jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用wordpress并尝试在文本输入的值大于40时引发消息.

I am using wordpress and trying to throw a message when the value for a text input is greater than 40..

<input type="text" name="quantity" size="2" value="<?php echo wpsc_cart_item_quantity(); ?>" />

现在,我想在此文本字段包含的值大于40并且还希望将其值重置为"时发出一条消息(警报) 我的WordPress主题使用jQuery 1.71 我正在执行以下操作:

Now i want to throw a message (alert) when this text field contains value greater than 40 and also want to reset its value to '' My wordpress theme uses jquery 1.71 I am doing the following:

jQuery("input[type='text'][name='quantity']").change(function() {
if (this.val >= 41) {
    alert("To order quantity greater than 40 please use the contact form.");
    this.val == '';
    this.focus();
    return false;
}
});

谢谢.

推荐答案

您首先需要使用$函数包装this,因为这是选择查询返回的DOM元素,而不是jQuery对象. ,而val()函数在该类型的对象上不可用.

You need in first place to wrap this with the $ function, since this is the DOM element returned by the selection query and not a jQuery object, and the val() function is not available on that kind of object.

此外,您需要使用括号来调用val()函数,否则,您需要处理函数的主体,而不是函数返回的值.

Also, you need to call the val() function with parenthesis, otherwise you're working on the body of the function, not the value it returns.

最后,赋值语句上有一个错字.您至少应该执行this.val = '',但是由于val是一个函数,而不是一个变量,因此将不起作用.工作代码应如下所示:

Finally, there is a typo on the assignment statement. You should at least do this.val = '', but that won't work since val is a function, not a variable. Working code should look like this:

$("input[type='text'][name='quantity']").change(function() {
    if ($(this).val() >= 41) {
        alert("To order quantity greater than 40 please use the contact form.");
        $(this).val('');
        $(this).focus();
    }        
});    

这篇关于文本输入值大于.. jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆