的方法之一將是「地圖」細胞的文本具有文本關鍵字的JavaScript的複雜陣列,然後鍵進行比較,以量的細胞量。如果比鍵更多的單元格,則表示存在具有相同文本的單元格。
代碼如下:
var allCells = $('#tb_cartTable tr td:nth-child(2)');
var textMapping = {};
allCells.each(function() {
textMapping[$(this).text()] = true;
});
var count = 0;
for (var text in textMapping)
count++;
if (count !== allCells.length) {
alert("found duplicate values");
} else {
alert("no duplicates found");
}
Live test case。
注意以上區分大小寫:如果有一個帶有「hello」的單元和帶有「Hello」的單元,那麼這些會被認爲是不同的,它會認爲沒有重複。如果不區分大小寫的解決方法是改變行的簡單情況:
textMapping[$(this).text().toLowerCase()] = true;
Updated test case它忽略大小寫。
在特定情況下,你可以將所有的普通數組的附加價值,然後使用jQuery方法檢查數組:
var $addedProductCodes = [];
$("#button_addItem").click(function(event)
{
$("span.errorText").remove();
$(".errorField").addClass("notErrorField");
//Change background color of textbox to normal
$("#frmRegisterForm :input[type='text']").attr('class','notErrorField');
$hasError = false;
$ele = $(event.target);
if($ele.is("input[type=button]"))
{
$td_productCode1=$("#td_productCode1").val();
var index = $.inArray($td_productCode1, $addedProductCodes);
if (index >= 0) {
alert("You already added this product code in line #" + (index + 1));
} else {
$text_productDescription= $("#text_productDescription").val();
$text_basicDealerPrice = $("#text_basicDealerPrice").val();
$('#table_viewContent').append("<tr><td>"+$text_productDescription+"</td><td>"+$td_productCode1+"</td><td><td>"+$text_basicDealerPrice+"</td><td><input type='button' name='deleteRow' id='btn_deleteRow' value='Delete' id='deleteItem' class='deleteItem button-red'></td></tr>");
$addedProductCodes.push($td_productCode1);
}
}
});
Updated fiddle其中添加相同的產品代碼會給警覺,並會不插入。
是否有任何錯誤?你能用jsfiddle.net初始化一個完整的例子嗎? – 2013-02-19 11:09:07
請發佈整個代碼... atleast那些你正在定義$ productCode,$ rowcounter,標誌 – bipen 2013-02-19 11:10:21
http://jsfiddle.net/rinuthomas90/ZXvQb/這是我的要求,當我點擊添加按鈕,價值追加到我的jsp頁面..我們不能接受重複的值到表。 – 2013-02-19 14:42:23