[英]jQuery selecting multiple classes but changing .css for one of them on current instance
[英]jQuery - Selecting a div with multiple classes, but searching for just two of them
我的標題可能不清楚,所以我會盡力解釋。
我有一個注冊表單,正在檢查是否所有必填字段都已填寫。 對於表單的一個特定部分,我有一個包含7或8個類的div,如果要查找其中兩個,則需要該div來顯示。
這就是我試圖做的不起作用的事..(這在使用兩個特定的類時會起作用,但在同時存在額外的類時也不會。)
的HTML
<div class="errors last first address city state zip country email phone" style="display: none;"><span class="error">Required values are missing from your contact information.</span></div>
jQuery的
$('form#finalize').submit(function() {
var required = ['hostname','first','last','address','city','state','zip','country','email','phone'];
var result = true;
$.each(required, function(index, value) {
if(filledElement(value) == false){
$(".item.errors").show().fadeOut(8000);
result = false;
}
});
if(checkElement('tos') == false){
$(".item.errors").show().fadeOut(8000);
return false;
}
return result;
});
function filledElement(elem){
if( isSet(elem) != true ) {
$('.'+elem+'.errors').show();
return false;
}
else {
$('.'+elem+'.errors').remove();
return true;
}
}
所以再次澄清..問題出在函數fillElement中,當它顯示錯誤時,“。'+ elem +'。errors”正在尋找僅包含這兩個類的元素,而不是尋找一個元素可能包含這兩個類別,甚至更多。
有什么解決辦法嗎? 我可以像...
if(需要的項目).. $('。errors.first.last.address.phone ...等等').show(); 但這似乎沒有必要,並且如果要更改模板也不是很靈活。
不,那不是你的問題。 您不需要將所有類都包含在選擇器中,只需將其中兩個類都可以正常工作。 演示: jsfiddle.net/Guffa/fj95m/
問題出在filledElement
函數的else
語句中。 如果填寫了任何字段,則刪除錯誤消息元素。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.