开发者

How to change "selected" value in combobox using JavaScript?

I have 2 comboboxes, "combo1" and "combo2",

If I select any value in combo1, I need that same value in combo2 as "selected" by JavaScript. What JavaScript code should be added to the HTML below to make it work?

<html>
    <head>
        <title>combo</title>
    </head>

    <body>
        <form name="testForm" action="" method="post">
            <select name="combo1">
                <option value="1">option 1</option>
                <option value="2">option 2</option>
                <option va开发者_如何转开发lue="3">option 3</option>
            </select><br /><br />
            <select name="combo2">
                <option value="1">option 1</option>
                <option value="2">option 2</option>
                <option value="3">option 3</option>
            </select>
        </form>
    </body>
</html>


<script type='text/javascript'>
    window.onload = function() { BindEvent(); }

    function BindEvent()
    {
        var elemToBind = document.getElementById ( "cmb1" );
        elemToBind.onchange = function () { SetSel ( this ); }
    }
    function SetSel(elem)
    {
        var secondCombo = document.getElementById ( "cmb2" );
        secondCombo.value = elem.value;   
    }
</script>
<body>
    <select id="cmb1">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
    <select id="cmb2">
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
    </select>
</body>


onChange() can help <select name="combo1" onChange="changeCombo2(...)">

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜