如何避免重复选择的项目下拉,这是在gridview使用javascript/jquery



我在gridview (html中的表)中有下拉菜单(在html中选择)。这个网格视图有多行。我的目标是不允许用户在下拉列表中选择相同的项目。

例如,如果用户选择newyork

分配房间

usernamedropdown        roomnumber
john                       1
john  (this  is wrong)     2

我想处理每个下拉菜单的onchange和循环通过网格中的所有值,如果任何下拉菜单的值与选择的值通过错误匹配。

what I try

<asp:Gridview id="grdtest" runat="server">
<asp:dropwnlist id="testid"  runat="server" onchange =" checkforvalue(this);">
</asp:Gridview>

JS

function checkforvalue()
{
var valuetocheck= $(testid).selectedvalue;

i am stuck here 
// loop through all rows in gridviewtable and find all the value of dropdown and test .

}

您可以遍历您的网格行并针对正确的列/单元格。

$('#grdtest tr').each(function(i){
   if($(this).find('td').eq(0).text() == valuetocheck){ 
       /* .eq(column index [0 is first]) - I am assuming here
          that your values are stored as strings in the table and 
          using .text() to pluck them out */
       /* duplicate value is present */
   } 
});

最新更新