admin管理员组文章数量:1220960
This is the jQuery code that I am using which helps me in getting the values for a select box I need when I change the first select box.
$(document).ready(function () {
$("#select1").change(function(){
$('.quantity').val('');
$('#trblock').fadeIn();
if ($(this).data('options') == undefined) {
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html('<option value="">').append(options);
});
});
What happens here is that whenever I change the select box value from 1st select box I get some values in the second select box. But when I roll back to the previously selected option in the 1st text box I find that the 2nd select box is already showing the options which I selected previously. What I want is whenever I change selecting the 1st select box option the 2nd select box should start with a blank option like <option value=""></option>
instead of the previously selected option in 2nd select box.
To make it easy I roll it in points.
Selected an option in 1st select box example "Animals".
<select name="" id="select1"> <option value=""></option> <option value="1">Animals</option> <option value="2">Games</option> <option value="3">Fruits</option> </select>
Got some values in 2nd select box.
<select name="" id="select2"> <option value=""></option> <option value="1">Cat</option> <option value="2">Dog</option> <option value="3">Monkey</option> </select>
Selected a value from the 2nd select box example "Dog".
Then I thought to switch the selection from 1st select box so I selected "Fruits".
Got some values in 2nd select box.
<select name="" id="select2"> <option value=""></option> <option value="1">Apple</option> <option value="2">Banana</option> <option value="3">Grapes</option> </select>
I selected grapes this time. Ok, fine till here.
But then if I again change my 1st selection to animals I should get the 2nd select box to start with the empty option (blank field) but Instead it starts with the previously selected "Dog". This is what I DO NOT want. Each time I change selecting the option from 1st select box I want second select box to start with the blank option field so that I can again choose from the full list i.e., "Cat", "Dog", "Monkey".
This is the jQuery code that I am using which helps me in getting the values for a select box I need when I change the first select box.
$(document).ready(function () {
$("#select1").change(function(){
$('.quantity').val('');
$('#trblock').fadeIn();
if ($(this).data('options') == undefined) {
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html('<option value="">').append(options);
});
});
What happens here is that whenever I change the select box value from 1st select box I get some values in the second select box. But when I roll back to the previously selected option in the 1st text box I find that the 2nd select box is already showing the options which I selected previously. What I want is whenever I change selecting the 1st select box option the 2nd select box should start with a blank option like <option value=""></option>
instead of the previously selected option in 2nd select box.
To make it easy I roll it in points.
Selected an option in 1st select box example "Animals".
<select name="" id="select1"> <option value=""></option> <option value="1">Animals</option> <option value="2">Games</option> <option value="3">Fruits</option> </select>
Got some values in 2nd select box.
<select name="" id="select2"> <option value=""></option> <option value="1">Cat</option> <option value="2">Dog</option> <option value="3">Monkey</option> </select>
Selected a value from the 2nd select box example "Dog".
Then I thought to switch the selection from 1st select box so I selected "Fruits".
Got some values in 2nd select box.
<select name="" id="select2"> <option value=""></option> <option value="1">Apple</option> <option value="2">Banana</option> <option value="3">Grapes</option> </select>
I selected grapes this time. Ok, fine till here.
But then if I again change my 1st selection to animals I should get the 2nd select box to start with the empty option (blank field) but Instead it starts with the previously selected "Dog". This is what I DO NOT want. Each time I change selecting the option from 1st select box I want second select box to start with the blank option field so that I can again choose from the full list i.e., "Cat", "Dog", "Monkey".
1 Answer
Reset to default 14Add $('#select2').val('')
at the last of select1
's change event like following.
$("#select1").change(function () {
$('.quantity').val('');
$('#trblock').fadeIn();
if ($(this).data('options') == undefined) {
$(this).data('options', $('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[value=' + id + ']');
$('#select2').html('<option value="">').append(options);
$('#select2').val(''); //add this line
});
本文标签: javascriptjQuery set select box to empty optionStack Overflow
版权声明:本文标题:javascript - jQuery set select box to empty option - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739274605a2155968.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论