Dynamic dropdown

后端 未结 2 1180
耶瑟儿~
耶瑟儿~ 2020-12-19 19:53

I have two dropdowns in my site. I want to change the number of options and the values of options of one dropdown depending on the value we choosing on the other dropbox by

相关标签:
2条回答
  • 2020-12-19 20:05

    Try this.

    HTML

    <select name="language" id="language" onchange="SetMedia(this)">
        <option>select</option>
        <option >English</option>
        <option>Tamil</option>
        <option>Telugu</option>
    </select>
    
    <select name="media" id="media" disabled="disabled">
        <option>select</option>
    </select>
    

    JavaScript

    function SetMedia(objLanguage) {
        var objMedia = document.getElementById("media");
        objMedia.options.length = 0;
            objMedia.disabled = false;
        switch (objLanguage.value) {
        case "English":
            objMedia.options.add(new Option("The Indian Express"));
            objMedia.options.add(new Option("The Hindu"));
            break;
        case "Tamil":
            objMedia.options.add(new Option("Tamil Paper 1"));
            objMedia.options.add(new Option("Tamil Paper 2"));
            break;
        case "Telugu":
            objMedia.options.add(new Option("Telugu Paper 1"));
            objMedia.options.add(new Option("Telugu Paper 2"));
            break;
        default:
            objMedia.options.add(new Option("select"));
            objMedia.disabled = true;
            break;
        }
    }
    

    Demo here: http://jsfiddle.net/naveen/z48dc/

    0 讨论(0)
  • 2020-12-19 20:13

    If you have to do this just plain javascript than you can do this.Which will result in little dirty approach .

    http://www.felgall.com/jstip22.htm

    I would suggest to do this using JQUERY and AJAX . Send a AJAX request to get the second dropdown data . Check this sample

    http://www.99points.info/2010/06/ajax-tutorial-dynamic-loading-of-combobox-using-jquery-and-ajax-in-php/

    http://demos.99points.info/dynamic_dropdown/

    0 讨论(0)
提交回复
热议问题