0

I am trying to work out how to show a div based on input to a field. I want to show the div if both of these conditions are true:

1) the field value is <=30 2) the field is not empty

at the moment i i have figured out the <=30 bit but then the form assumes that empty is 0 and shows the div, but i dont want the div to show if the field is left blank.

I tried this but it doesnt work:

 $("#hidden")[$(this).val() <= "30" and !="" ? 'show' : 'hide']("fast");

Here's what i have so far: http://jsfiddle.net/q5kKz/318/

Thanks for any help!

4

4 回答 4

3

Fix the syntax :

 var val = this.value;
 $("#hidden")[(val !=='' && val <= 30) ? 'show' : 'hide']("fast");

Demonstration (I fixed a few other problems)

于 2013-05-24T13:55:27.240 回答
0

也许问题是 30 是一个字符串,所以你需要将它转换为 int,就像这样

$("#hidden")[parseInt($(this).val()) <= 30 && !="" ? 'show' : 'hide']("fast");

希望它对你有用!

于 2013-05-24T13:57:14.527 回答
0

演示--> http://jsfiddle.net/q5kKz/322/

&&不使用and

$('#Severity-of-your-symptoms').change(function() {
  $("#li-10-14")[this.value <= "30" && this.value != '' ? 'show' : 'hide']("fast");
}).change();
于 2013-05-24T13:58:04.790 回答
0

试试这个:

http://jsfiddle.net/q5kKz/323/

这个“===”在字符串比较中很重要:

$(this).val() !== "" && $(this).val()*1 <= 30

您还应该考虑使用其他事件,例如按键,否则 JS 只会在失去焦点时触发。

于 2013-05-24T13:59:49.693 回答