简体   繁体   English

使用jQuery获取属性值

[英]Get attribute value with jQuery

I'm trying to get the attribute value in jQuery, but isn't working, My code reports undefined. 我正在尝试在jQuery中获取属性值,但无法正常工作,我的代码报告未定义。 Here is an example: 这是一个例子:

 console.log($(".btn-causas input").find(".active input").attr('d')); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div class="btn-group btn-causas" data-toggle="buttons"> <label class="btn btn-info "> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="security_alert">Security </label> <label class="btn btn-info "> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="strike">Strike </label> <label class="btn btn-info active"> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="dontknow">I DON'T </label> </div> 

The selector could be simply done like : 选择器可以像这样简单地完成:

$(".btn-causas .active input").attr('d');

Hope this helps. 希望这可以帮助。

 console.log( $(".btn-causas .active input").attr('d') ); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="btn-group btn-causas" data-toggle="buttons"> <label class="btn btn-info "> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="security_alert"> Security </label> <label class="btn btn-info "> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="strike"> Strike </label> <label class="btn btn-info active"> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="dontknow"> I DON'T </label> </div> 

The best way to do that is that you rename the attribute d for data-name and you can get very easy that value with this code: 最好的方法是将属性d重命名为data-name并且可以通过以下代码轻松获得该值:

Html: HTML:

<input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" data-name="dontknow">

Jquery: jQuery:

var value = $('input[name="options"]:checked').data('name');
alert(value);

This is the best way to do that, this code retrieve the value of a checked input radio with the attribute data-name 这是最好的方法,此代码使用属性data-name检索已检查的输入单选的值。

Regards! 问候!

You're specifying wrong selector .btn-causas input , instead use this .btn-causas , See the code below: 您指定的选择器.btn-causas input错误,而是使用此.btn-causas ,请参见以下代码:

Make jQuery find the inputs inside div - .btn-causas instead of finding it inside inputs, as you've done. 让jQuery的找到inputs内部DIV - .btn-causas ,而不是发现这里面的投入,因为你所做的一切。 Inputs doesn't contains a child elements, so you can't find elements inside it. 输入不包含子元素,因此您无法在其中找到元素。

 $(function() { console.log($(".btn-causas").find(".active input").attr('d')); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="btn-group btn-causas" data-toggle="buttons"> <label class="btn btn-info "> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="security_alert"> Security </label> <label class="btn btn-info "> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="strike"> Strike </label> <label class="btn btn-info active"> <input type="radio" name="options" id="optiond1" class="btncauses" autocomplete="off" d="dontknow"> I DON'T </label> </div> 

Hope this helps! 希望这可以帮助!

您没有活动的课程,但是根据我的理解,您正在尝试获取选定的单选按钮的属性,因此请尝试:检查输入

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

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