获取所选的< option>的值在ddSlick下拉列表中 [英] Get value of selected <option> in ddSlick dropdown
问题描述
这个当前返回未定义。在注释行中应该怎么提醒当前< option>
标签的值(1,2,3或4)?
This currently returns undefined. What should go in the commented line to alert the value (1, 2, 3 or 4) of the current <option>
tag?
<select id="dropdown" name="dropdown">
<option value="0" data-imagesrc="images/icons/all.png">All Questions</option>
<option value="1" id="friends" data-imagesrc="images/icons/friends.png">Friends</option>
<option value="2" data-imagesrc="images/icons/friends_of_friends.png">Friends of Friends</option>
<option value="3" data-imagesrc="images/icons/network.png"><?php echo $network; ?></option>
<option value="4" data-imagesrc="images/icons/location.png"><?php echo $location ?></option>
</select>
<script type="text/javascript">
$('#dropdown').ddslick({
showSelectedHTML: false,
onSelected: function(selectedData){
var str = $(this).attr('id'); // WHAT SHOULD GO HERE?
alert(str);
}
});
</script>
编辑
如果相关,我正在使用此插件。
If it's relevant, I'm using this plugin.
也许这个问题可能有帮助。我试图理解它。
Perhaps this question might help. I'm trying to make sense of it.
管理来计算出来。最终工作代码是:
Managed to figure this out. Final working code is:
<select id="dropdown" name="dropdown" value="hello">
<option value="0" data-imagesrc="images/icons/all.png">All Questions</option>
<option value="1" id="friends" data-imagesrc="images/icons/friends.png">Friends</option>
<option value="2" data-imagesrc="images/icons/friends_of_friends.png">Friends of Friends</option>
<option value="3" data-imagesrc="images/icons/network.png"><?php echo $network; ?></option>
<option value="4" data-imagesrc="images/icons/location.png"><?php echo $location ?></option>
</select>
<script type="text/javascript">
$('#dropdown').ddslick({
showSelectedHTML: false,
onSelected: function(data){
alert(data.selectedData.value);
}
});
</script>
推荐答案
根据您的插件文档, onSelected
方法获取 selectedData
参数:
According to the docs for your plugin, the onSelected
method gets the selectedData
parameter:
selectedData(带有文本,值,description,imageSrc的嵌套对象)
selectedData (nested object with text, value, description, imageSrc)
文本标签和值可用作 selectedData.text
和 selectedData.value
在 onSelected
函数内。尝试这样:
The text label and value are available as selectedData.text
and selectedData.value
inside the onSelected
function. Try this:
$('#dropdown').ddslick({
showSelectedHTML: false,
onSelected: function(selectedData){
var str = selectedData.value
alert(str);
}
});
这篇关于获取所选的< option>的值在ddSlick下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!