jQuery - 获取输入属性并在输入后将其添加为文本

时间:2015-11-13 18:42:36

标签: jquery html

这是我的HTML代码:

<div class="list">
    <input checked="checked" data-blacklist="[392,340,393]" data-parentarticleid="249" data-price="0" data-price-included="1" data-string-value="2x500GB SATA2" id="article_249" name="hardwareSetHD" type="radio" value="249"><label for="article_249">2x500GB SATA2 <span></span> </label>
</div>
<div class="list">
    <input checked="checked" data-blacklist="[392,340,393]" data-parentarticleid="249" data-price="10" data-price-included="1" data-string-value="4x500GB SATA2" id="article_241" name="hardwareSetHD" type="radio" value="241"><label for="article_241">2x500GB SATA2 <span></span> </label>
</div>
<div class="list">
    <input checked="checked" data-blacklist="[392,340,393]" data-parentarticleid="242" data-price="20" data-price-included="1" data-string-value="8x500GB SATA2" id="article_242" name="hardwareSetHD" type="radio" value="242"><label for="article_242">8x500GB SATA2 <span></span> </label>
</div>

我想要什么?

在输入广播后,文档在<span></span>中加载到ADD后,文本在data-price中保留。

这是我试过的:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $('input[type=radio][name=hardwareSetRAM]').this(text($(this).attr( "data-price" )));
});
</script>

那么如何从名称为data-price的每个输入中获取hardwareSetHD并在输入后将其添加为文本?

提前致谢!

1 个答案:

答案 0 :(得分:2)

您需要遍历每一行,获取价格值并更新范围标记。

$(document).ready(function($) {

    $('.list').each(function() {

        var inp = $(this).find('input[type=radio]'),
            span = $(this).find('span');

        var price = inp.data('price');

        span.text( price );

    });

});

jQuery.data()