简体   繁体   English

如何从内联样式属性中获取值并将其放入最接近的输入值中? jQuery的

[英]How can I get a value from an inline style attribute and put inside the closest input value? jQuery

How can I get a value from an inline style attribute, remove the '%', and place it inside the closest (or the next) input value attribute? 如何从内联样式属性中获取值,删除'%',然后将其放置在最近(或下一个)输入值属性中?

<div>
    <div>
       <div style="width: 52%;"></div>
    </div>
    <input type="hidden" name="item_1" value="0">
</div>

<div>
    <div>
       <div style="width: 52%;"></div>
    </div>
    <input type="hidden" name="item_2" value="0">
</div>

I need to get the " 52 " of each <div style="width: 52%;"> and put it inside the closest input value replacing the "0". 我需要获取每个<div style="width: 52%;">的“ 52 ”,并将其放在最接近的输入值中,以替换“ 0”。 How can I do this in jQuery? 如何在jQuery中做到这一点? Thanks 谢谢

$('div[style]').each(function () {
    $(this).parent().next().val( parseFloat(this.style.width) );
});

Here's the fiddle: http://jsfiddle.net/7GA4n/ 这是小提琴: http : //jsfiddle.net/7GA4n/

Try this: http://jsfiddle.net/z9FSb/ 试试这个: http : //jsfiddle.net/z9FSb/

$('div[style]').each(function(){
    var str = $(this).attr('style');
    var nstr = str.substr(str.indexOf(':')+1).slice(0, -2);
    var Val = $(this).parent().next('input').val(nstr);
    alert($('[type="hidden"]').val());
});

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

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