最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery - How to grab specific data-type of option after that option is selected? - Stack Overflow

programmeradmin4浏览0评论

I'll jump straight with the markup, then explain what I'm trying to do.

Html select options

    <select id="1d" name="camp">
    <option data-url="week_1" value="Week 1">30th July</option>
    <option data-url="week_2" value="Week 2">6th August</option>
    </select>
    <input type="hidden" name="camp_url" id="1e">

Jquery script I'm struggling with. The script below shows the value of the selected option, of course, but I can't find a way to grab to the data-url info.

    $('#1d').change(function () {
        $('#1e').val($(this).val());
    });

Bottom line, I'm needing the value of input#1e to be the data-url of #1d upon selection.

Thanks for your help.

I'll jump straight with the markup, then explain what I'm trying to do.

Html select options

    <select id="1d" name="camp">
    <option data-url="week_1" value="Week 1">30th July</option>
    <option data-url="week_2" value="Week 2">6th August</option>
    </select>
    <input type="hidden" name="camp_url" id="1e">

Jquery script I'm struggling with. The script below shows the value of the selected option, of course, but I can't find a way to grab to the data-url info.

    $('#1d').change(function () {
        $('#1e').val($(this).val());
    });

Bottom line, I'm needing the value of input#1e to be the data-url of #1d upon selection.

Thanks for your help.

Share Improve this question asked Jun 5, 2012 at 20:57 Stephen CallenderStephen Callender 5381 gold badge5 silver badges18 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

How about that?

$("#1d").on("change", function () {
    var url = $(this).children(":selected").data("url");
    $("#1e").val(url);
});

DEMO: http://jsfiddle/aRzNn/


Also personally I enjoy this solution:

$("#1d").on("change", function () {
    $("#1e").val(function() {
        return $("#1d > :selected").data("url");
    });
});​
发布评论

评论列表(0)

  1. 暂无评论