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
Add a ment  | 

5 Answers 5

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