javascript 在 ddSlick 下拉列表中获取所选 <option> 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13995178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value of selected <option> in ddSlick dropdown
提问by Sebastian
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?
这当前返回未定义。注释行中应该添加什么来提醒当前<option>
标签的值(1、2、3 或 4)?
<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>
EDIT
编辑
If it's relevant, I'm using this plugin.
如果相关,我正在使用这个插件。
Perhaps this questionmight 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>
采纳答案by Mathletics
According to the docs for your plugin, the onSelected
method gets the selectedData
parameter:
根据插件的文档,该onSelected
方法获取selectedData
参数:
selectedData (nested object with text, value, description, imageSrc)
selectedData(带有文本、值、描述、imageSrc 的嵌套对象)
The text label and value are available as selectedData.text
and selectedData.value
inside the onSelected
function. Try this:
文本标签和值可作为selectedData.text
和selectedData.value
内部onSelected
功能。试试这个:
$('#dropdown').ddslick({
showSelectedHTML: false,
onSelected: function(selectedData){
var str = selectedData.value
alert(str);
}
});
回答by techfoobar
Use $(this).val()
in place of $(this).attr('id')
$(this).val()
代替使用$(this).attr('id')
The value of the currently selected <option>
is returned when you call .val()
on the <select>
element.
<option>
当您调用.val()
该<select>
元素时,将返回当前选定的值。
回答by Zbigniew
$('#dropdown').ddslick({
showSelectedHTML: false,
onSelected: function(selectedData){
var str = $(this).val()
alert(str);
}
});
So, use val()
instead of attr('id')
所以,使用val()
代替attr('id')
回答by Gerrit de Vries
I got it!! if you want to get the attribute value you set in your array where your data is coming from you get your value like this->
我知道了!!如果你想获得你在你的数据来自的数组中设置的属性值,你可以像这样获得你的值->
$('#id').ddslick({
data:dropdowndata,
width:60,
selectText: "Select Circle",
imagePosition:"left",
//dropdowndata[4].selected:true
onSelected: function(selectedData){
val = selectedData.selectedIndex;
alert(dropdowndata[val].value);
回答by kudzai zishumba
if you want to post the data, create a hidden input, use jquery to set the value of the hidden field then post the hidden field
如果要发布数据,创建一个隐藏输入,使用jquery设置隐藏字段的值然后发布隐藏字段
$('.ddslick_drop_down').ddslick({
onSelected: function(selectedData){
//set the value to a hidden field then post the hidden field
$('#dish_id').val(selectedData.selectedData.value);
$('.ddslick_drop_down').ddslick({
onSelected: function(selectedData){
//set the value to a hidden field then post the hidden field
$('#dish_id').val(selectedData.selectedData.value);
回答by Gerrit de Vries
I got the selected index from ddslick and assinged a value to seperate input >fields.this way I can have input fields to control my form. I've been coding >for 3months now so i don't really know what i'm doing. This is a long way of >doing it but it seems to work so...
我从 ddslick 获得了选定的索引,并分配了一个值来单独输入>字段。这样我就可以有输入字段来控制我的表单。我已经编码 > 3 个月了,所以我真的不知道我在做什么。这是 > 做它的很长的路要走,但它似乎奏效了......
onSelected: function(selectedData){
alert("selectedData.selectedIndex");
if(selectedData.selectedIndex == 1 ){
$("#input1").html(selectedData.selectedIndex);
}
if(selectedData.selectedIndex == 2 ){
$("#input2").html(selectedData.selectedIndex);
}
if(selectedData.selectedIndex == 3 ){
$("#input3").html(selectedData.selectedIndex);
}
}