[英]jQuery get element attribute from nested element
我處於以下情況:
<span class="inputgrp">
<span class="additem">
</span>
</span>
<input type="text" avl="abc">
我正在嘗試通過class =“ inputgrp ”上的click事件從輸入中獲取屬性avl的值
我嘗試過:
$(".additem").click(function () {
var v = $(this).parent().find("input:text").attr("avl")
})
和
$(".inputgrp").click(function () {
var v = $(this).prev("span").next("input:text").attr("avl")
})
但是沒有成功。 希望能得到一些指導,因為我不知道我錯了什么。
$(".inputgrp").click(function() { alert($(this).next("input:text").attr("data-avl")) })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <span class="inputgrp"> <span class="additem"> 1 </span> </span> <input type="text" data-avl="abc">
.next()
因為輸入在click元素旁邊 .find
搜索當前元素的子級。 該input
是不是一個child
,但一個sibling
的.inputgrp
$('.inputgrp').on('click',function() {
var v = $(this).siblings('input[data-avl]').attr('data-avl');
});
data-avl
作為屬性使其有效。 .sibling()
函數選擇任何同級 input
元素始終是下一個元素,請使用.next()
函數 在html輸入元素上使用data-avl=""
屬性,在jQuery中使用.data('avl')
獲得屬性的值
$(".inputgrp").click(function() {
alert($(this).next("input:text").data("avl"));
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.