After Ajax Request Select Option not Resetting
up vote
0
down vote
favorite
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
add a comment |
up vote
0
down vote
favorite
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
try$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid ofdisabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
I have a form in which there is a title, select option, and content. What I am doing is I am taking all the data and sending it to the server via ajax, I am able to reset the input fields but I am not able to reset the select option box.
following is my select box code:
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
Following is the jQuery code that I am using to reset the above field:
// $('.post_category').prop('selectedIndex',-1);
// $('.post_category').val($(this).find('option:first').val());
// $("select.post_category option:selected").attr('disabled','disabled');
// $("select").val("");
$('.post_category').prop('selectedIndex',0);
The commented code is the codes I've used it. What I want to do is, when the page loads by default it shows in select Select Post Category
. I want to display the same after the ajax request when I reset it. However, I am finding difficulties in it. What's wrong I am not able to get it.
jquery
jquery
asked Nov 10 at 23:08
Akshay Shrivastav
411421
411421
try$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid ofdisabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16
add a comment |
try$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid ofdisabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16
try
$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
try
$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
1
Can't select a disabled option. Get rid of
disabled
. Can simplify also by just setting value of select ... $('.post_category').val('')
– charlietfl
Nov 10 at 23:16
Can't select a disabled option. Get rid of
disabled
. Can simplify also by just setting value of select ... $('.post_category').val('')
– charlietfl
Nov 10 at 23:16
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
up vote
1
down vote
accepted
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
I give you an example that approach your goal. When document loads, after 1 second
the select is changed to America
, and 4 seconds
later will be changed again to the default value.
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
But, since you are using the select2 plugin, the best approach for what you want is to use the placeholder, not a default disabled option. You can check this on next example:
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
$(".select2").select2();
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option selected disabled value="">Select Post Category</option>
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
$(document).ready(function()
$(".select2").select2(
placeholder: "Select Post Category",
allowClear: true
);
setTimeout(
function()$("#id_h5_single").val("Am").change();,
1000
);
setTimeout(
function()$("#id_h5_single").val("").change();,
5000
);
);
.select2
width: 50vw;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/css/select2.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.5/js/select2.min.js"></script>
<select name="post_category" class="post_category round select2 form-control" id="id_h5_single">
<option value="Am">America</option>
<option value="Nt">Netherland</option>
</select>
answered Nov 10 at 23:50
D. Smania
2,6441321
2,6441321
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted
$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
hi @D. Smania, understood the logic you explained and then i modified my code a bit so this one worked perfectly as i wanted
$(".post_category").val("").change();
– Akshay Shrivastav
Nov 11 at 9:11
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53244311%2fafter-ajax-request-select-option-not-resetting%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
try
$('.post_category option[value=""]').prop('selected',true);
– Mohamed-Yousef
Nov 10 at 23:16
1
Can't select a disabled option. Get rid of
disabled
. Can simplify also by just setting value of select ...$('.post_category').val('')
– charlietfl
Nov 10 at 23:16