admin管理员组文章数量:1296470
I'm using the jquery-select2 plugin, and I have the following field that is being autopopulated by AJAX:
<input type="hidden" id="player2" class="form-control select2">
Here is the javascript:
$('#player2').select2({
placeholder: "Select an opponent",
allowClear: false,
ajax: {
dataType: "json",
url: "getUsers.php",
data: function (term) {
return {
q: term, // search term
};
},
results: function (data) {
console.log(data);
return {results: data};
},
}
});
console.log($("#player2").select2("val"));
The data, as shwon in the console.log within the results function, is structured like this: [{"id":"[email protected]", "text":"someone"}]
After the choice is selected, trying to console.log($("#player2").select2("val")) gives me the ID, but I can't seem to get the text. None of the following works to get the text value of "someone" in this case and I don't see where I'm going wrong.
$("#player2 option:selected").text()
$("#player2 option:selected").select2().text()
$("#player2").text()
I'm using the jquery-select2 plugin, and I have the following field that is being autopopulated by AJAX:
<input type="hidden" id="player2" class="form-control select2">
Here is the javascript:
$('#player2').select2({
placeholder: "Select an opponent",
allowClear: false,
ajax: {
dataType: "json",
url: "getUsers.php",
data: function (term) {
return {
q: term, // search term
};
},
results: function (data) {
console.log(data);
return {results: data};
},
}
});
console.log($("#player2").select2("val"));
The data, as shwon in the console.log within the results function, is structured like this: [{"id":"[email protected]", "text":"someone"}]
After the choice is selected, trying to console.log($("#player2").select2("val")) gives me the ID, but I can't seem to get the text. None of the following works to get the text value of "someone" in this case and I don't see where I'm going wrong.
$("#player2 option:selected").text()
$("#player2 option:selected").select2().text()
$("#player2").text()
Share
Improve this question
edited Feb 14, 2017 at 8:47
apokryfos
40.7k11 gold badges81 silver badges125 bronze badges
asked Nov 10, 2014 at 18:37
bo_knowsbo_knows
8662 gold badges9 silver badges21 bronze badges
0
5 Answers
Reset to default 4 $("#player2").select2('data')[0].id;
$("#player2").select2('data')[0].value;
$("#player2").select2('data').text
Using version 3.5.1
// this will give you value of selected element.
$("#player2").val();
I have used form tag and using submit event I have captured id of value.
$("#Selector").submit(function (event) {
console.log($("#Selector").serialize())
});
//other option is below
$("#select_machine").select2('data')[0].text
.on("select2:select", function(e) {
console.log(e.params.data.id);
});
本文标签: javascriptjqueryselect2 obtaining ID and text from a select boxStack Overflow
版权声明:本文标题:javascript - jquery-select2 obtaining ID and text from a select box? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741636877a2389688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论