当我从下拉列表中选择项目时,如果用户再次添加该项目,它将添加到表格中,它应显示已添加项目的错误。
<select id="dropdownUsers"></select>
<table border="1" id="tableshow">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>unit Price</td>
<td>qunatity</td>
<td>total</td>
<td>Actions</td>
</tr>
</thead>
<tbody id="tabledata"></tbody>
</table>
ajax调用绑定表
var val = $("#dropdownUsers").val();
$.ajax({
dataType: 'json',
type: 'POST',
contentType: "application/Json; charset=utf-8",
url: '/Home/fillDropdownbyids',
data: JSON.stringify({ id: val }),
success: function (response) {
$("#id").append(response.user.iUserInfoId + " ")
var tr = '';
tr += '<tr class="row"><td class="rowid' + val + '">' + response.user.iUserInfoId + '</td><td>' + response.user.strFirstName + '</td><td><input type="text" id="unittextbox' + val + '"></td><td><input type="text" id="quantextbox' + val + '" onfocusout="get(' + val + ')"></td><td><input type="text" id="totaltextbox' + val + '" ></td><td><input type="button" value="Delete" onclick="deleteRow(this)"></td></tr>';
$('#tabledata').append(tr);
},
});
这将从表中获取第一个td的行,它将返回id
数组的列表 $('#tableshow > tbody > tr ').each(function (index, td) {
arr = $(this).find('td');
arrlist[index] = arr[0].innerHTML;
index++;
});
我想比较这个td值和下拉选择值如果匹配那么它应该显示记录存在,否则它允许将数据插入表
答案 0 :(得分:0)
试试这个:
var val = $("#dropdownUsers").val();
var isAvailiable = myFunction(val);
if(isAvailiable){
$.ajax({
dataType: 'json',
type: 'POST',
contentType: "application/Json; charset=utf-8",
url: '/Home/fillDropdownbyids',
data: JSON.stringify({ id: val }),
success: function (response) {
$("#id").append(response.user.iUserInfoId + " ")
var tr = '';
tr += '<tr class="row"><td class="rowid' + val + '">' + response.user.iUserInfoId + '</td><td>' + response.user.strFirstName + '</td><td><input type="text" id="unittextbox' + val + '"></td><td><input type="text" id="quantextbox' + val + '" onfocusout="get(' + val + ')"></td><td><input type="text" id="totaltextbox' + val + '" ></td><td><input type="button" value="Delete" onclick="deleteRow(this)"></td></tr>';
$('#tabledata').append(tr);
},
})
} else {
//Show error message
}
//Call this before calling myFuction();
$('#tableshow > tbody > tr ').each(function (index, td) {
arr = $(this).find('td');
arrlist[index] = arr[0].innerHTML;
index++;
});
//Function to check that value is present array or not
function myFunction(val){
var isAvailiable = false
for(i=0;i<arrlist.length;i++){
if(arrlist[i] === val){
isAvailiable = true;
}
}
return isAvailiable;
}