javascript - Read the data-option value on select - Stack Overflow

Using JQuery, How can I read the data-option value when an option is clickedselected from List.<spa

Using JQuery, How can I read the data-option value when an option is clicked/selected from List.

<span data-name="" data-select="dropdown" class="btn-group dropdown dropdown-select">
    <a data-target="#" data-toggle="dropdown" class="btn dropdown-toggle">Select User</a>
    <a data-target="#" data-toggle="dropdown" class="btn dropdown-toggle end"><i class="icon-arrow-down"></i></a>
    <ul class="dropdown-menu">        
        <li class="active"><a data-option="1D" href="#">One Day</a></li>
        <li class=""><a data-option="1W" href="#">One Week</a></li>
        <li class=""><a data-option="2W" href="#">Two Weeks</a></li>

     </ul>
</span>

Using JQuery, How can I read the data-option value when an option is clicked/selected from List.

<span data-name="" data-select="dropdown" class="btn-group dropdown dropdown-select">
    <a data-target="#" data-toggle="dropdown" class="btn dropdown-toggle">Select User</a>
    <a data-target="#" data-toggle="dropdown" class="btn dropdown-toggle end"><i class="icon-arrow-down"></i></a>
    <ul class="dropdown-menu">        
        <li class="active"><a data-option="1D" href="#">One Day</a></li>
        <li class=""><a data-option="1W" href="#">One Week</a></li>
        <li class=""><a data-option="2W" href="#">Two Weeks</a></li>

     </ul>
</span>
Share Improve this question asked Jul 24, 2013 at 16:53 Ritesh Kumar GuptaRitesh Kumar Gupta 5,1917 gold badges51 silver badges71 bronze badges 4
  • 1 Are you using bootstrap? – David Hellsing Commented Jul 24, 2013 at 16:59
  • the question is tagged twitter-bootstrap, you can assume he does ;) – Pascamel Commented Jul 24, 2013 at 17:03
  • @Pascamel It wasn't originally. – thatidiotguy Commented Jul 24, 2013 at 17:04
  • @thatidiotguy my bad ;) i opened the question right between your question and david's questionm sorry for that guys! – Pascamel Commented Jul 24, 2013 at 17:05
Add a ment  | 

4 Answers 4

Reset to default 2
$("ul.dropdown-menu a").click(function(){
    alert($(this).data("option"));
});

Try this on

$("ul.dropdown-menu a").on("click",function(){
     alert($(this).data("option"));
});

As click is not working when element added after dom is loaded.

You can do

<li class="active"><a data-option="1D" href="#" onclick="getDataOption(this);">One Day</a></li>

var getDataOption = function (node) {
    var value = node.getAttribute("data-option");
    console.log(value);
    return value;
};
$("ul.dropdown-menu li").click(function(){

   alert($(this).attr("data-option"));
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745336680a4623135.html

相关推荐

  • javascript - Read the data-option value on select - Stack Overflow

    Using JQuery, How can I read the data-option value when an option is clickedselected from List.<spa

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信