最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can i add extra field on this select2 form - Stack Overflow

programmeradmin2浏览0评论

I am trying to get more field filled with a select2 drop-down menu. Check this fiddle: /

I want the "Select extra" to be populated too. But with a different value.

Now I can print id & text, I want to store and print one more value "extra"

html:

<input type="hidden" id="test" />
<p>Selected IDs: <input type="text" id="selectedID"/></p>
<p>Selected Options: <input typ="text" id="selectedText"/></p>
<p>Selected Extra Field: <input typ="text" id="selectedExtra"/></p>

js:

var test = $('#test');
$(test).select2({
placeholder: "Choose",
data:[
    {id:0,text:"enhancement"},
    {id:1,text:"bug"},
    {id:2,text:"duplicate"},
    {id:3,text:"invalid"},
    {id:4,text:"wontfix"}
],
width: "300px"
});

$(test).change(function() {

var theID = $(test).select2('data').id;
var theSelection = $(test).select2('data').text;
var theExtra = $(test).select2('data').text;
$('#selectedID').val(theID);
$('#selectedText').val(theSelection);
$('$selectedExtra').val(theExtra);
});

I am trying to get more field filled with a select2 drop-down menu. Check this fiddle: http://jsfiddle/ws3tjp5f/

I want the "Select extra" to be populated too. But with a different value.

Now I can print id & text, I want to store and print one more value "extra"

html:

<input type="hidden" id="test" />
<p>Selected IDs: <input type="text" id="selectedID"/></p>
<p>Selected Options: <input typ="text" id="selectedText"/></p>
<p>Selected Extra Field: <input typ="text" id="selectedExtra"/></p>

js:

var test = $('#test');
$(test).select2({
placeholder: "Choose",
data:[
    {id:0,text:"enhancement"},
    {id:1,text:"bug"},
    {id:2,text:"duplicate"},
    {id:3,text:"invalid"},
    {id:4,text:"wontfix"}
],
width: "300px"
});

$(test).change(function() {

var theID = $(test).select2('data').id;
var theSelection = $(test).select2('data').text;
var theExtra = $(test).select2('data').text;
$('#selectedID').val(theID);
$('#selectedText').val(theSelection);
$('$selectedExtra').val(theExtra);
});
Share Improve this question edited Mar 13, 2019 at 9:00 Gufran Hasan 9,4117 gold badges43 silver badges55 bronze badges asked Aug 13, 2014 at 9:50 user3801032user3801032 1
  • Im using php and JSON in my actual code. – user3801032 Commented Aug 13, 2014 at 9:52
Add a ment  | 

2 Answers 2

Reset to default 6

I created a Js Fiddle for this, you also have a typo on $('$selectedextra'), it should be $('#selectedextra'), basically i just added a new column on the JSON data called 'extra'.

    var test = $('#test');
    $(test).select2({
        placeholder: "Choose",
        data:[
            {id:0,text:"enhancement",extra:"one"},
            {id:1,text:"bug",extra:"two"},
            {id:2,text:"duplicate",extra:"three"},
            {id:3,text:"invalid",extra:"four"},
            {id:4,text:"wontfix",extra:"five"}
        ],
        width: "300px"
    });

    $(test).change(function() {

        var theID = $(test).select2('data').id;
        var theSelection = $(test).select2('data').text;
        var theExtra = $(test).select2('data').extra;

        $('#selectedID').val(theID);
        $('#selectedText').val(theSelection);
        $('#selectedExtra').val(theExtra);

    });
var theID = $(test).select2('data').id;
var theSelection = $(test).select2('data').text;
var theExtra = $(test).select2('data').text;
$('#selectedID').val(theID);
$('#selectedText').val(theSelection);
-->    $('#selectedExtra').val(theExtra);
});

this line

$('#selectedExtra').val(theExtra);
发布评论

评论列表(0)

  1. 暂无评论