我的表單驗證有問題: 我有兩個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