jquery - 通过变量选择attr

时间:2011-07-29 11:44:08

标签: jquery variables attributes

var attr = myopts.addAttributeFromChemSelect;
alert(ob.attr(attr));

未定义

var attr = myopts.addAttributeFromChemSelect;
alert(attr);

是'tip-ref'

var attr = myopts.addAttributeFromChemSelect;
alert(ob.attr('tip-ref');

是'tip_0'

任何人都可以向我解释为什么第一个不会工作。

问候

来自Consol Log:

style="width: 100px; position: relative;" tip-ref="tip_0" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlSalutation" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlSalutation" onchange="SalutationChangeEvent_cphDoubleColumn_checkout_checkout_customer();"
tip-ref
undefined
tip_0
style="width: 170px; position: relative;" tip-ref="tip_4" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlGender" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlGender"
tip-ref
undefined
tip_4
style="width: 50px; position: relative;" tip-ref="tip_5" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlDOBDate" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlDOBDate"
tip-ref
undefined
tip_5
style="width: 100px; position: relative;" tip-ref="tip_6" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlDOBMonth" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlDOBMonth"
tip-ref
undefined
tip_6
style="width: 50px; position: relative;" tip-ref="tip_7" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlDOBYear" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlDOBYear"
tip-ref
undefined
tip_7
style="width: 170px; position: relative;" tip-ref="tip_12" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlOccupationStatus" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlOccupationStatus"
tip-ref
undefined
tip_12
style="width: 100px; position: relative;" tip-ref="tip_13" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlYearsWithEmployer" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlYearsWithEmployer"
tip-ref
undefined
tip_13
style="width: 100px; position: relative;" tip-ref="tip_14" name="ctl00$cphDoubleColumn$checkout$checkout_customer$ddlMonthsWithEmployer" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_customer_ddlMonthsWithEmployer"
tip-ref
undefined
tip_14
style="width: 100px; position: relative;" tip-ref="tip_22" name="ctl00$cphDoubleColumn$checkout$addressUC1$ddlYearsAtAddress" onchange="javascript:__CLDVcphDoubleColumn_checkout_lightboxProcessAddresses='cphDoubleColumn_checkout_addressUC1_ddlYearsAtAddress';Page_ValidationActive=false;setTimeout('__doPostBack(\'ctl00$cphDoubleColumn$checkout$addressUC1$ddlYearsAtAddress\',\'\')', 0)" class="chemSelectWrapper w_cphDoubleColumn_checkout_addressUC1_ddlYearsAtAddress"
tip-ref
undefined
tip_22
style="width: 100px; position: relative;" tip-ref="tip_23" name="ctl00$cphDoubleColumn$checkout$addressUC1$ddlMonthsAtAddress" onchange="javascript:__CLDVcphDoubleColumn_checkout_lightboxProcessAddresses='cphDoubleColumn_checkout_addressUC1_ddlMonthsAtAddress';Page_ValidationActive=false;setTimeout('__doPostBack(\'ctl00$cphDoubleColumn$checkout$addressUC1$ddlMonthsAtAddress\',\'\')', 0)" class="chemSelectWrapper w_cphDoubleColumn_checkout_addressUC1_ddlMonthsAtAddress"
tip-ref
undefined
tip_23
style="width: 170px; position: relative;" tip-ref="tip_24" name="ctl00$cphDoubleColumn$checkout$addressUC1$ddlResidentialStatus" class="chemSelectWrapper w_cphDoubleColumn_checkout_addressUC1_ddlResidentialStatus"
tip-ref
undefined
tip_24
style="width: 100px; position: relative;" tip-ref="tip_36" name="ctl00$cphDoubleColumn$checkout$checkout_BankDetails$ddlYearsWithBank" onchange="javascript:setTimeout('__doPostBack(\'ctl00$cphDoubleColumn$checkout$checkout_BankDetails$ddlYearsWithBank\',\'\')', 0)" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_BankDetails_ddlYearsWithBank"
tip-ref
undefined
tip_36
style="width: 100px; position: relative;" tip-ref="tip_37" name="ctl00$cphDoubleColumn$checkout$checkout_BankDetails$ddlMonthsWithBank" class="chemSelectWrapper w_cphDoubleColumn_checkout_checkout_BankDetails_ddlMonthsWithBank"
tip-ref
undefined
tip_37

代码在插件中:

chemSelectboxRevert: function (opts, callbackFnk) {

            var myopts = $.extend({ keepSelected: true, addAttributeFromChemSelect : '' }, opts);

            var count = this.length;

            return this.each(function (i) {

                var ob = $(this);

                var attr = myopts.addAttributeFromChemSelect;

                console.log(getAttributes(ob));  console.log(attr); console.log(ob.attr(attr)); console.log(ob.attr('tip-ref'));

                var extraAttr = '';

                if (myopts.addAttributeFromChemSelect != '') {

                    var attr_extra = ob.attr(myopts.addAttributeFromChemSelect);

                    extraAttr = myopts.addAttributeFromChemSelect + '="' + attr_extra + '"';

                }
        ....
        ...
        ..

2 个答案:

答案 0 :(得分:2)

实际上它有效:http://jsfiddle.net/fBLZR/

答案 1 :(得分:0)

与Darin Dimitrov相同,我的测试工作正常。什么是addAttributeFromChemSelect