我试图看看是否在选择框中选择了一个选项,如果没有,我希望它提醒一个字符串.我指的是这个链接(
Check if option is selected with jQuery,if not select a default),但它没有用.
这是我的代码:
- <select id="language" name="language">
- <option value=""></option>
- <option value="1">One</option>
- <option value="2">Two</option>
- <option value="3">Three</option>
- </select>
- if(!$("#language option:selected").length) {
- alert('no option is selected');
- }
我几乎复制了链接的答案,但它仍然没有用.我错过了什么?
解决方法
另一种方法是:
- if($("#language").attr("selectedIndex") == 0) {
- alert("You haven't selected anything!");
- }