我的表单验证有问题: 我有两个DIV-容器,每个容器有两个答案。我想验证是否有最低限度检查一个无线电或有一个文本元素的值。选择器不起作用
但我的jQuery选择不工作:
HTML:
<div class="mandatory">
<input type="radio" value="1" name="answer1" /> Option 1
<input type="radio" value="2" name="answer1" /> Option 2
</div>
<div class="mandatory">
<input type="radio" value="1" name="answer2" /> Option 1
<input type="text" name="answer2" />
</div>
JS:
$('.mandatory').each(function(){
var elem = $(this).find('input:checked, input:text[value!=""]');
console.log(elem.attr('name')); //Always "answer2"
});
它返回的输入元素虽然是空的。 这是我的代码:http://fiddle.jshell.net/Pisi2012/n9S2Y/
感谢您的帮助!
元=输入,输入=文本和值不应该是空的 – alexP
@Bondye:不完全。见http://api.jquery.com/text-selector – BoltClock
answer2返回undefined,undefined不等于'' – Kayo