简体   繁体   English

从输入文本获取属性值

[英]Get attribute value from input text

I want to get attribute value of each input field. 我想获取每个输入字段的属性值。

<input type="text" id="qty" data-id="{{$item->rowid}}" value="{{$item->qty}}">

function goUpdateCart(){

    $('input[type=text]').each(function(){

        alert($('input[type=text]').getAttribute("data-id"));
    })
}

My function can't work correctly 我的功能无法正常工作

Well, .getAttribute("data-id"); 好吧, .getAttribute("data-id"); isn't jquery method. 不是jquery方法。 You can use any of the following to get the data-* attr : 您可以使用以下任何一种方法来获取data-* attr

$(this).data('id');
$(this).attr('data-id');
$(this).prop('dataset').id;
this.dataset.id;  

So it could be any of the above one: 因此可能是以上任何一个:

function goUpdateCart(){

    $('input[type=text]').each(function(){

        alert(this.dataset.id);
    })
}

Try this: 尝试这个:

$('input[type=text]').each(function(){
     alert($(this).attr("data-id"));
});

You need to use each iteration element context this in each function to target current element along with .attr('data-id') or .data('id') : 您需要在每个函数中使用每个迭代元素上下文this目标对象与.attr('data-id').data('id')

$('input[type=text]').each(function(){
    alert($(this).data("id"));
});
<input type="text" id="qty" data-id="{{$item->rowid}}" value="{{$item->qty}}">
function goUpdateCart()
{

   var attribute=$('#qty').attr('data-id');
   alert(attribute);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM