【问题标题】:jQuery made up attribute not getting valuejQuery组成的属性没有获得价值
【发布时间】:2012-10-04 14:59:29
【问题描述】:

嘿,我正在尝试获取我制作的单击的自定义单选按钮的值。

这里是按钮的js:

$("#df-opt0,#df-opt1,#df-opt2,#df-opt3,#df-opt4").click(function (input) {
    $('#df-opt0').css("background-image", "url(img/radiobutton-OFF.png)");
    $('#df-opt1').css("background-image", "url(img/radiobutton-OFF.png)");
    $('#df-opt2').css("background-image", "url(img/radiobutton-OFF.png)");
    $('#df-opt3').css("background-image", "url(img/radiobutton-OFF.png)");
    $('#df-opt4').css("background-image", "url(img/radiobutton-OFF.png)");
    //alert($(this).attr('id'));
    $('#' + $(this).attr('id')).css("background-image", "url(img/radiobutton-ON.png)");
    //globalRadioSelected = $(this).attr('id');

    fValue =  $(input).attr("store-id");
    alert('the ID :'+fValue);
});

这是 HTML:

<div id="df-opt0" store-id="100"></div>
<div id="df-opt1" store-id="200"></div>
<div id="df-opt2" store-id="300"></div>
<div id="df-opt3" store-id="400"></div>
<div id="df-opt4" store-id="500"></div>
<div id="df-opt4txt" store-id="600">
    <input type="text" id="df-opt4txt-txt" style="width: 130px;" class="formBoxes">
</div>

当我点击其中任何一个时,我得到的是:ID:未定义

我错过了什么?

【问题讨论】:

标签: jquery custom-attributes attr


【解决方案1】:

你应该使用“this”ala:

fValue =  $(this).attr("store-id");

【讨论】:

    【解决方案2】:

    使用fValue = $(this).attr("store-id"); 而不是fValue = $(input).attr("store-id");

    传递给单击处理函数的参数是单击事件本身,它显然没有store-id 属性。

    在这种情况下,this 对象将与input.target(点击事件的目标 DOM 元素)相同。

    【讨论】:

      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 2016-10-20
      • 2021-03-28
      相关资源
      最近更新 更多