I am using the select2 plugin. How to make the selected option after clicking on the SEND not fought?











up vote
0
down vote

favorite












I have such a problem .. in the localStorage of the Sahranet there is only one selected element and after append at it is duplicated



<form method='GET'>
<select class='select' multiple='multiple'>

<option value='a2'>aladin</option>
<option value='3s'>ark</option>
<option value='4a'>asdasdtaktak</option>
<option value='5f'>ametis</option>

</select>

<input type='submit' value='send'>

</form>


$(document).ready(function($){



     $('.select').select2({});
$('.select').on('select2:select', function (e) {
var data = e.params.data;
testObject={
'id': data.id,
'text':data.text
}

localStorage.setItem("lastname", JSON.stringify(testObject) );

});

if(localStorage.getItem('lastname')){

var data= JSON.parse(localStorage.getItem('lastname'));

console.log(data.id );
var newOption = new Option(data.text, data.id, false, true);
$('.select').append(newOption).trigger('change');
}



});









share|improve this question






















  • Try new Option(data.text, data.id, false, false); to not select the option when appending: developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/…
    – Niloct
    Nov 10 at 17:44










  • in this case, I can not see the selected item
    – Artak
    Nov 10 at 17:59










  • I solved the problem like this..$('.select').val(data.id).trigger('change');.))).. but save to localStorage multiple items fail((
    – Artak
    Nov 10 at 18:10















up vote
0
down vote

favorite












I have such a problem .. in the localStorage of the Sahranet there is only one selected element and after append at it is duplicated



<form method='GET'>
<select class='select' multiple='multiple'>

<option value='a2'>aladin</option>
<option value='3s'>ark</option>
<option value='4a'>asdasdtaktak</option>
<option value='5f'>ametis</option>

</select>

<input type='submit' value='send'>

</form>


$(document).ready(function($){



     $('.select').select2({});
$('.select').on('select2:select', function (e) {
var data = e.params.data;
testObject={
'id': data.id,
'text':data.text
}

localStorage.setItem("lastname", JSON.stringify(testObject) );

});

if(localStorage.getItem('lastname')){

var data= JSON.parse(localStorage.getItem('lastname'));

console.log(data.id );
var newOption = new Option(data.text, data.id, false, true);
$('.select').append(newOption).trigger('change');
}



});









share|improve this question






















  • Try new Option(data.text, data.id, false, false); to not select the option when appending: developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/…
    – Niloct
    Nov 10 at 17:44










  • in this case, I can not see the selected item
    – Artak
    Nov 10 at 17:59










  • I solved the problem like this..$('.select').val(data.id).trigger('change');.))).. but save to localStorage multiple items fail((
    – Artak
    Nov 10 at 18:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have such a problem .. in the localStorage of the Sahranet there is only one selected element and after append at it is duplicated



<form method='GET'>
<select class='select' multiple='multiple'>

<option value='a2'>aladin</option>
<option value='3s'>ark</option>
<option value='4a'>asdasdtaktak</option>
<option value='5f'>ametis</option>

</select>

<input type='submit' value='send'>

</form>


$(document).ready(function($){



     $('.select').select2({});
$('.select').on('select2:select', function (e) {
var data = e.params.data;
testObject={
'id': data.id,
'text':data.text
}

localStorage.setItem("lastname", JSON.stringify(testObject) );

});

if(localStorage.getItem('lastname')){

var data= JSON.parse(localStorage.getItem('lastname'));

console.log(data.id );
var newOption = new Option(data.text, data.id, false, true);
$('.select').append(newOption).trigger('change');
}



});









share|improve this question













I have such a problem .. in the localStorage of the Sahranet there is only one selected element and after append at it is duplicated



<form method='GET'>
<select class='select' multiple='multiple'>

<option value='a2'>aladin</option>
<option value='3s'>ark</option>
<option value='4a'>asdasdtaktak</option>
<option value='5f'>ametis</option>

</select>

<input type='submit' value='send'>

</form>


$(document).ready(function($){



     $('.select').select2({});
$('.select').on('select2:select', function (e) {
var data = e.params.data;
testObject={
'id': data.id,
'text':data.text
}

localStorage.setItem("lastname", JSON.stringify(testObject) );

});

if(localStorage.getItem('lastname')){

var data= JSON.parse(localStorage.getItem('lastname'));

console.log(data.id );
var newOption = new Option(data.text, data.id, false, true);
$('.select').append(newOption).trigger('change');
}



});






php jquery jquery-select2






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 17:25









Artak

1




1












  • Try new Option(data.text, data.id, false, false); to not select the option when appending: developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/…
    – Niloct
    Nov 10 at 17:44










  • in this case, I can not see the selected item
    – Artak
    Nov 10 at 17:59










  • I solved the problem like this..$('.select').val(data.id).trigger('change');.))).. but save to localStorage multiple items fail((
    – Artak
    Nov 10 at 18:10


















  • Try new Option(data.text, data.id, false, false); to not select the option when appending: developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/…
    – Niloct
    Nov 10 at 17:44










  • in this case, I can not see the selected item
    – Artak
    Nov 10 at 17:59










  • I solved the problem like this..$('.select').val(data.id).trigger('change');.))).. but save to localStorage multiple items fail((
    – Artak
    Nov 10 at 18:10
















Try new Option(data.text, data.id, false, false); to not select the option when appending: developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/…
– Niloct
Nov 10 at 17:44




Try new Option(data.text, data.id, false, false); to not select the option when appending: developer.mozilla.org/en-US/docs/Web/API/HTMLOptionElement/…
– Niloct
Nov 10 at 17:44












in this case, I can not see the selected item
– Artak
Nov 10 at 17:59




in this case, I can not see the selected item
– Artak
Nov 10 at 17:59












I solved the problem like this..$('.select').val(data.id).trigger('change');.))).. but save to localStorage multiple items fail((
– Artak
Nov 10 at 18:10




I solved the problem like this..$('.select').val(data.id).trigger('change');.))).. but save to localStorage multiple items fail((
– Artak
Nov 10 at 18:10

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241545%2fi-am-using-the-select2-plugin-how-to-make-the-selected-option-after-clicking-on%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241545%2fi-am-using-the-select2-plugin-how-to-make-the-selected-option-after-clicking-on%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues