I have 3 select drop down box in my webpage, When i select the options in first select box, then the options of second and third should filter thier options, I have tried a lot, My result is Iam get Change in One select box alone and the other select box is empty. I have given my code below please help me
Code:
<script type="text/javascript">//<![CDATA[
$(window).load(function(){
$("#select1").change(function() {
if($(this).data('options') == undefined){
/*Taking an array of all options-2 and kind of embedding it on the select1*/
$(this).data('options',$('#select2 option').clone());
}
var id = $(this).val();
var options = $(this).data('options').filter('[id=' + id + ']');
$('#select2').html(options);
var options = $(this).data('options').filter('[id=' + id + ']');
$('#select3').html(options);
});
});//]]>
</script>
<form action="send_request.php" name="myForm1" method="post" onsubmit="return(validate1());">
<select class="regtext sec1test" name="select" id="select1" style="width:99%">
<option value="0">Select What?</option>
<option value="1">Features</option>
<option value="2">Video</option>
<option value="3">Buy1</option>
<option value="4">Buy2</option>
</select>
<div class="sec1 lef" >
<select name="type" class="regtext sec1test" id="select2" style="width:99%">
<option id="0" value="0">Select genre</option>
<option value="1" id="1">abc</option>
<option value="2" id="2">models</option>
<option value="3" id="3">Comp</option>
<option value="4" id="4">Beat</option>
</select>
</div>
<div class="sec1 lef" >
<select name="type" class="regtext sec1test" id="select3" style="width:99%">
<option id="0" value="0">Select genre</option>
<option value="1" id="1">abc</option>
<option value="2" id="2">mod</option>
<option value="3" id="3">Comp</option>
<option value="4" id="4">Beat</option>
</select>
</div>
</form>
What is the error in my code
Here is the solutions:
jQuery.fn.filterByText = function(textbox, selectSingleMatch) {
return this.each(function() {
var select = this;
var options = [];
$(select).find('option').each(function() {
options.push({value: $(this).val(), text: $(this).text()});
});
$(select).data('options', options);
$(textbox).bind('change keyup', function() {
var options = $(select).empty().data('options');
var search = $(this).val().trim();
var regex = new RegExp(search,"gi");
$.each(options, function(i) {
var option = options[i];
if(option.value.match(regex) !== null) {
$(select).append(
$('<option>').text(option.text).val(option.value)
);
}
});
if (selectSingleMatch === true && $(select).children().length === 1) {
$(select).children().get(0).selected = true;
}
});
});
};
$(function() {
$('#select2').filterByText($('#select1'), false);
$('#select3').filterByText($('#select1'), false);
});
The plugin was taken from this site: http://www.lessanvaezi.com/filter-select-list-options/
This is how I would do it. Note that I'm filtering by data-id
, ids
must be unique and are not suitable for this type of functionality.
I dont know the specifics of the inner workings, but I'm pretty sure that this is a byproduct of having duplicate ids and using the filter function.
var $select1=$("#select1"); var $select2=$("#select2"); var $select3=$("#select3"); $select1.data('options', $select2.html()) $select1.change(function(){ var val=$select1.val(); var options = $($select1.data('options')).filter('[data-id="'+val+'"]'); $select2.html(options); $select3.html(options.clone()); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="send_request.php" name="myForm1" method="post" onsubmit="return(validate1());"> <select class="regtext sec1test" name="select" id="select1" style="width:99%"> <option value="0">Select What?</option> <option value="1">Features</option> <option value="2">Video</option> <option value="3">Buy1</option> <option value="4">Buy2</option> </select> <div class="sec1 lef"> <select name="type" class="regtext sec1test" id="select2" style="width:99%"> <option id="0" value="0">Select genre</option> <option value="1" data-id="1">abc</option> <option value="2" data-id="2">models</option> <option value="3" data-id="3">Comp</option> <option value="4" data-id="4">Beat</option> </select> </div> <div class="sec1 lef"> <select name="type" class="regtext sec1test" id="select3" style="width:99%"> <option id="0" value="0">Select genre</option> <option value="1" data-id="1">abc</option> <option value="2" data-id="2">mod</option> <option value="3" data-id="3">Comp</option> <option value="4" data-id="4">Beat</option> </select> </div> </form>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.