问题
Please how can I populate combo box based on input value from textbox using HTML/PHP
E.g. *if textbox value equals "a" then combo box value equals (a,b,c,d) else if textbox value equals "b" then combo box value equals (e,f,g,h)
any help is highly welcome
回答1:
this script will do it.
$("#TextInputID").change(function(){
var optionsForA = ['a','b','c','d'];
var optionsForB = ['e','f','g','h'];
var options = '';
var inputVal = $("#TextInputID").val();
switch(inputVal) {
case "a":
for (var i=0;i<optionsForA.length;i++){
options += '<option value="'+ optionsForA[i] + '">' + optionsForA[i] + '</option>';
}
break;
case "b":
for (var i=0;i<optionsForB.length;i++){
options += '<option value="'+ optionsForB[i] + '">' + optionsForB[i] + '</option>';
}
break;
}
$('#comboboxid').find('option').remove().end().append(options);
});
回答2:
It's fairly simple and easy. There are two ways of doing it
Either do it on client side using javascript/jQuery by putting an onchange listener on the textbox each time it's value is changed check if it's a then place specific options in combobox, if it's b put other concerned options in combobox and so on ...
You can do it using PHP and AJAX as well, if textbox has a value like 'a' so truncate comboxbox options and using ajax fetch certain options from the database and append them to the comboxbox.
Need more explanation? hit me back :)
来源:https://stackoverflow.com/questions/36972669/how-to-automatically-change-values-of-combobox-value-based-on-input-value-in-tex