简体   繁体   English

jQuery设置attr,然后获取attr

[英]jQuery Set attr and then Get attr

I am creating a page that sets 2 attributes and then I am trying to reference those attributes after the page has loaded. 我正在创建一个设置2个属性的页面,然后我试图在页面加载后引用这些属性。

I have been able to set the attributes without a problem, I have also been able to reference the attributes if I hard code them into the credit and debit attributes. 我已经能够毫无问题地设置属性,如果我将它们硬编码到信用和借记属性中,我也能够引用这些属性。

But trying to call them dynamically isn't working. 但试图动态调用它们是行不通的。 Open to suggestions. 接受建议。

<div class="net" credit="" debit=""></div>


$(document).ready(function() {
    ... some fancy code involving arrays and junk ...
    if(TYPE == 'credit') {
        $(".net").attr('credit',data.response.stats.total);
    } else if (TYPE = 'debit') {
       $(".net").attr('debit',data.response.stats.total);
    }
});

$(window).bind("load", function() {
    afterPageLoad();
});

function afterPageLoad(){
    total_credit = $(".net").attr('credit');
    total_debit = $(".net").attr('debit');
    total = (total_credit - total_debit);
    $(".net").html( "$"+total );
}

Suggest using data- attributes that can be read by jQuery.data() 建议使用jQuery.data()可以读取的data-属性

<div id="test" data-foo="bar"></div>

GET 得到

alert( $('#test').data('foo'))

SET

$('#test').data('foo','new value, or object or array'))

The attributes don't have to exist to use this. 不必存在使用此属性的属性。 You can store anything any time on an element, as well as read data in markup. 您可以随时在元素上存储任何内容,也可以在标记中读取数据。

jQuery.data() API Docs jQuery.data()API文档

The problem is the "... some fancy code involving arrays and junk ..." 问题是“......一些涉及数组和垃圾的奇特代码......”

data.response suggests you are making an Ajax call which is asynchronous and you are trying to read the attributes before they are set. data.response表明您正在进行异步的Ajax调用,并且您尝试在设置之前读取属性。

Put the logic in the callback of the Ajax call, do not call in the onload portion. 将逻辑放在Ajax调用的回调中,不要在onload部分调用。

To read dynamic attribute values, you need to use prop() instead of attr() . 要读取动态属性值,需要使用prop()而不是attr()

Ideally, however you should store custom values in a data- attribute (ie data-credit ) and then use jQuery's data() method to get/set the value. 理想情况下,您应该将自定义值存储在data-属性(即data-credit )中,然后使用jQuery的data()方法来获取/设置值。

When grabbing the attributes for total_credit and total_debit you should use parseFloat(); 当抓取total_credittotal_debit的属性时,你应该使用parseFloat();

ex: parseFloat( $(".net").attr('credit') ); 例如: parseFloat( $(".net").attr('credit') );

you should also set a fallback in the case the attribute isn't set. 您还应该在未设置属性的情况下设置回退。

ex: total_credit = parseFloat($(".net").attr('credit')) || 0; 例如: total_credit = parseFloat($(".net").attr('credit')) || 0; total_credit = parseFloat($(".net").attr('credit')) || 0;

$(document).ready(function() {
    var TYPE = 'credit';
    if(TYPE == 'credit') {
        $(".net").attr('credit', 123);
    } else if (TYPE = 'debit') {
       $(".net").attr('debit', 53);
    }
});

$(window).bind("load", function() {
    afterPageLoad();
});

function afterPageLoad(){
    total_credit = parseFloat($(".net").attr('credit'))||0;
    total_debit = parseFloat($(".net").attr('debit'))||0;
    total = (total_credit - total_debit);
    $(".net").html( "$"+total );
}

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

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