Jquery - 如何在选择该选项后获取特定数据类型的选项? [英] Jquery - How to grab specific data-type of option after that option is selected?
本文介绍了Jquery - 如何在选择该选项后获取特定数据类型的选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我会直接跳过标记,然后解释我正在尝试做什么。
I'll jump straight with the markup, then explain what I'm trying to do.
Html选择选项
<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脚本我正在努力。下面的脚本显示了所选选项的值,当然,我找不到一种方法来获取 data-url
info。
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());
});
底线,我需要值
输入#1e
为
#1d $ c
data-url
$ c>一经选择。
Bottom line, I'm needing the value
of input#1e
to be the data-url
of #1d
upon selection.
感谢您的帮助。
推荐答案
那怎么样?
$("#1d").on("change", function () {
var url = $(this).children(":selected").data("url");
$("#1e").val(url);
});
DEMO: http://jsfiddle.net/aRzNn/
我个人也喜欢这个解决方案:
Also personally I enjoy this solution:
$("#1d").on("change", function () {
$("#1e").val(function() {
return $("#1d > :selected").data("url");
});
});
这篇关于Jquery - 如何在选择该选项后获取特定数据类型的选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文