ใช้ tag select เพื่อให้ dropdown list 2 อันเชื่อมกัน (อันนี้ใช้งานได้ดี) Populate a select dropdown list with jQuery
หน้าแรก jQuery ใช้ tag select เพื่อให้ dropdown list 2 อันเชื่อมกัน (อันนี้ใช้งานได้ดี) Populate a select dropdown list with jQuery
Its common to meet situations where you want a dropdown list to be updated depending on a visitors input.
Lets say we have a contact form and we want to list contact names according to the department the visitor selects. Namely, in our case, well have different contact names for the sales and support departments.
<select name="dep">
<option>Department</option>
<option>Sales</option>
<option>Support</option>
</select>
<select name="cname">
<option>Contact name</option>
</select>
</form>
Both select fields have a name attribute, so we can find and select them with jQuery. The logic of the functions well build is pretty simple:
IF the Sales option is selected, then add some names to the second select input, and the same goes for the other departments.
So lets get right on it!
Well build all of our code inside a jQuery ready function so the code will only be run after the entire HTML elements (the DOM) have finished loading.
// code here
});
To avoid making jQuery go through the DOM over and over again when well fetch our select inputs, well add the selects to variables:
$department = $("select[name='dep']");
$cname = $("select[name='cname']");
});
From now on, whether well use $(select[name='dep']) or $department, it will mean the same thing, difference is though, jQuery will no longer traverse the document again with $department, it will just know where that element is. This is very important for larger scripts that may slow down a web page.
Now that weve added our select inputs to variables, well use jQuerys onChange function to detect when our department current selection changes.
$department = $("select[name='dep']");
$cname = $("select[name='cname']");
$department.change(function() {
// code here
});
});
We have our onChange function, now its just a matter of a few if-and-else conditions to know when a certain department is selected, so lets start with the Sales department.
$department = $("select[name='dep']");
$cname = $("select[name='cname']");
$department.change(function() {
//if the value from department selection is Sales then
if ($(this).val() == "Sales") {
$("select[name='cname'] option").remove(); //remove all options from Contact names
$("").appendTo($cname); //add an option to Contact names
$("").appendTo($cname); //add an option to Contact names
}
});
});
I believe the code is pretty self-explanatory, weve checked if the selected option from department is Sales and removed all options from the second selection and added two new names. The very same if-and-else condition will go for the rest of the departments.
Final code:
$department = $("select[name='dep']");
$cname = $("select[name='cname']");
$department.change(function() {
if ($(this).val() == "Sales") {
$("select[name='cname'] option").remove();
$("").appendTo($cname);
$("").appendTo($cname);
}
if ($(this).val() == "Support")
{
$("select[name='cname'] option").remove();
$("").appendTo($cname);
$("").appendTo($cname);
}
if ($(this).val() == "Department")
{
$("select[name='cname'] option").remove();
$("").appendTo($cname);
}
});
});
Once you understand the logic, the code wont look as hard.
Still have questions? post them in the comments below!
refer: http://devingredients.com/2011/05/populate-a-select-dropdown-list-with-jquery/
ขึ้นไปด้านบน
