![](/img/trans.png)
[英]How to check element triggered by onBlur has certain class with jQuery
[英]jquery how to check near element with certain class exist?
我在<tr>
中有兩個元素:
<table>
<tbody>
<tr>
<td><input type="text" class="form-control input-state" onclick="myfunction(this)"></td>
<td><input type="text" class="form-control input-city" onclick="myfunction(this)"></td>
</tr>
</tbody>
</table>
在myfunction
中,我想檢查帶有 cartain 類的元素是否存在:
if($(thisElem).closest("tr").find(".input-city").length){
console.log('true')
}
但是$(thisElem).closest("tr").find(".input-city").length
總是0
! 如何檢查此元素的存在?
這是為了表明問題是由於無效的 html 造成的,因為 tr 應該像這個演示所示的那樣包裝在一個表中:
function myfunction(target){ if($(target).closest("tr").find(".input-city").length){ console.log('true'); }else{ console.log('false'); } }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1>Working</h1> <table> <tr> <td><input type="text" class="form-control input-state" onclick="myfunction(this)"></td> <td><input type="text" class="form-control input-city" onclick="myfunction(this)"></td> </tr> </table> <h1>Not Working</h1> <tr> <td><input type="text" class="form-control input-state" onclick="myfunction(this)"></td> <td><input type="text" class="form-control input-city" onclick="myfunction(this)"></td> </tr>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.