美好的一天,jQuery添加css類
我有一個以一行數據開頭的HTML表格。每行有一個下拉菜單和五個輸入文本框。在行的末尾是一個圖像,點擊時將克隆最後一行並刪除文本框中的任何數據。客戶希望能夠根據需要創建儘可能多的行。
我有一些JavaScript/jQuery循環徹底的每一行,並根據每個選擇/輸入控件的名稱屬性動態創建一個對象。此外,我想驗證數字字段(浮點數或整數)。 這是正常工作。問題是驗證失敗時,我想在輸入文本週圍繪製紅色邊框。
這裏是我的代碼:
$("input.submit").on('click', function(e) {
var billingDetailRows = [];
var detailRows = [];
$('.miBillingDetails tbody tr').each(function(i, row) {
// http://stackoverflow.com/questions/9132347/iterating-through-table-cells-using-jquery
// The 'this' is necessary to ensure you only iterate through the row you're currently on
var detailRow = {};
var isNumeric = false;
var dataType = '';
$('td', this).each(function(e) {
var domElement = $(this).find(':input');
var attrName = '';
if (typeof domElement != 'undefined') {
attrName = domElement.attr('name');
if (typeof attrName != 'undefined')
console.log(attrName);
}
var dataType = domElement.attr('data-type');
if (typeof dataType != 'undefined') {
isNumeric = dataType === 'numeric';
dataType = domElement.attr('data-format');
}
var value = $(this).find(':input').val();
if (typeof value != 'undefined') {
console.log(value === '' ? 'NONE' : value);
var actualValue = value === '' ? '' : value;
if (isNumeric && actualValue.length > 0) {
if (!validateNumber(actualValue, dataType === 'int')) {
$(domElement).css({
"border-color": "#FF0000",
"border-width": "1px",
"border-style": "solid"
});
// $(domElement).addClass("errorBorder");
}
}
detailRow[attrName] = actualValue;
}
});
detailRows.push(detailRow);
})
});
.errorBorder {
thin solid #FF0;
}
<table class="miBillingDetails">
<tr>
<td>
<select name="RevenueType">
<option value="Cash">Cash</option>
<option value="CC">Credit Card</option>
<option value="Visa">Visa</option>
</select>
</td>
<td>
<input type="text" name="BillingRate" data-type="numeric" data-format="float">
</td>
<td>
<input type="text" name="Quantity" data-type="numeric" data-format="int">
</td>
<td>
<input type="text" name="FlatRate" data-type="numeric" data-format="float">
</td>
<td>
<input type="text" name="SequenceStart">
</td>
<td>
<input type="text" name="SequenceEnd">
</td>
<td>
<img class="cloneWars" src="images/add2.jpg" alt="Add Tag" />
</td>
</tr>
</table>
的問題,我想用addClass/removeClass。但無論如何,這是行不通的。我必須設置CSS才能工作。
我在做什麼錯?
TIA,
COSON
你還試過''domElement.addClass(「errorBorder」);''? –
您確定您有該規則的有效css定義嗎?對我來說似乎不是這樣 –
@BettySt他有這樣的評論,我相信他已經試過 – stackoverfloweth