jQuery:检查是否已存在包含某些值的表行 [英] jQuery: Check to see if table row containing certain values exists already
问题描述
我有一个jQuery脚本,可以在表中附加一行。简而言之,用户可以从下拉菜单中选择一个值,然后输入x月份数量并点击添加,这会将行附加到表格中。例如,用户追加以下行:
I have a jQuery script that appends a row to a table. In short, the user can select a value from a drop down menu, then enter in 'x' amount of months and hits 'add', which appends the row to the table. For example the following row is appended by the user:
<tr>
<td>Some Value</td>
<td>2</td>
</tr>
现在,如果用户再次执行相同的操作,我需要停止程序并提醒他他试图添加一个重复的值。如何 - 使用jQuery - 我可以检查以上行是否已经存在,其中第一个和第二个< td>
元素具有与数据相同的值正在尝试添加?我之所以说第一个和第二个< td>
元素的原因是因为还有其他< td>
元素该行但它们包含隐藏的字段,这些字段将新添加的数据提交到服务器端脚本,因此我希望尽可能简短且简单地验证。
Now, if the user performs the same operation again, I need to stop the procedure and alert him that he is trying to add a duplicate value. How - using jQuery - can I check to see whether or not the above row already exists, where the first and second <td>
elements have the same value as the data he is trying to add? The reason I say first and second <td>
elements only is because there are other <td>
elements in the row but they house hidden fields which submit the newly added data to to a server side script, so I'd like to keep the validation as short and simple as possible.
如果有任何帮助,表ID为 #tblspecializations
If it is of any help, the table ID is #tblspecializations
非常感谢您的输入。
推荐答案
您或许可以使用 :contains() :
You might be able to use :contains():
$('#tblspecializations tr > td:contains(Some Value) + td:contains(2)').length
- 示例
请注意,:contains()将返回元素,如果给定的字符串匹配元素文本中的任何位置。
Be aware, though, that :contains() will return the element if the given string is matched anywhere in the text of the element.
这篇关于jQuery:检查是否已存在包含某些值的表行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!