How to automatically change values of combobox value based on input value in text box using php and html

荒凉一梦 提交于 2019-12-25 06:25:04

问题


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

  1. 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 ...

  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!