JQuery 根据“select”是否改变函数大于或小于给定值
此刻这似乎正在击败我,但我认为我不可能在一百万英里之外。
基本上我有一个年龄验证表单,它只显示年份,除非该年份的日月也会影响满足年龄限制的用户(在本例中为 1992 年 18 岁) - 在这种情况下,日/月选择也会出现(淡入)。
所以这是我的代码:
$(document).ready(function(){
$('#dateob').hide(); //hide the div containing the month and day selects
$("#f_yob").change(function() {
//select change function - if the value is greater than 1992 show the other select boxes
if ("$('#f_yob').attr('value')>1992") {
$('#dateob').fadeIn('slow');
}
//otherwise hide the day/month selects
else {
$('#dateob').fadeOut('slow');
}
});
});
如果有人能够告诉我哪里出错了,我将非常感激!
安迪
This seems to be defeating me at the moment but I don't think I can be a million miles away.
Basically I have a Age Verification form which just shows the year unless it's the year in which the day month would also affect the user meeting the age restriction, (in this case 1992 for 18) - in which case the day/month selects would also appear (fadeIn).
So here's the code I have:
$(document).ready(function(){
$('#dateob').hide(); //hide the div containing the month and day selects
$("#f_yob").change(function() {
//select change function - if the value is greater than 1992 show the other select boxes
if ("$('#f_yob').attr('value')>1992") {
$('#dateob').fadeIn('slow');
}
//otherwise hide the day/month selects
else {
$('#dateob').fadeOut('slow');
}
});
});
If anyone is able to tell me where I'm going wrong I'd really appreciate it!
Andy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您只需将
if
更改为:在
对于“怎么了?”主要是语法部分,
if()
中只有一个字符串,如果不为空,则true
就if()
而言> 就检查而言,您需要进行实际比较。You just need to change your
if
to this:In a
<select>
(or any other input type), you use.val()
to get the value then useparseInt()
since you're comparing it to a number (.val()
returns a string).For the "what's wrong?" part..syntax mainly, you just have a string in the
if()
, which if not empty istrue
as far as anif()
check is concerned, instead you need an actual comparison there.在代码的第五行中,您执行以下操作:
我相信您的意思是:
或者更好:
编辑 1
您可以使用
.val()
而不是.attr('value')
。编辑2:
添加了 parseInt
in the fifth line of your code you do something like this:
I believe what you meant is:
Or even better:
Edit 1
you can use
.val()
instead of.attr('value')
.Edit 2:
Added parseInt