在刷新页面时设置不同的选项选项 - JAVASCRIPT

时间:2013-03-25 05:46:12

标签: javascript html css

所以我有一张表如下:

<select id="CellLayout" style="color:#99FF00; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>

默认情况下,“选择布局”是显示的内容。点击一个按钮,我需要选择框来显示'自定义'。我试着搜索SO,但我试图在没有Jquery的情况下这样做..

4 个答案:

答案 0 :(得分:3)

试试这个......简单......真的很有用..

<script type="text/javascript">
function fun()
{

document.getElementById("CellLayout").selectedIndex = 4;
}
</script>


<form name="f1">
<select id="CellLayout" style="color:#99FF00; font-family:monospace;" >
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>
<input type="button" onclick="fun()"/>
</form>

答案 1 :(得分:2)

您可以更改选择值,如下所示:

<select id="CellLayout" style="color:#99FF00; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>

<input type="button" value="change" onclick="change()" />

<script type="text/javascript">
function change() {
    document.getElementById('CellLayout').value = 'custom';
}
</script>

FIDDLE

答案 2 :(得分:0)

你可以尝试一下:

<select id="CellLayout" style="color:#000; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option value="0">select layout</option>
    <option value="1">blinker</option>
    <option value="2">glider</option>
    <option value="3">flower</option>
    <option value="4">custom</option>
</select>
<input id="btn" type="submit" value="setSelected">
<script type="text/javascript">
    var btn = document.getElementById('btn');
    btn.onclick = function(){
        var layOut = document.getElementById('CellLayout');
        for(var i = 0; i < layOut.length; i++){
            if(layOut.options[i].value == '4'){
                layOut.selectedIndex = i;
            }
        }
    }
</script>

答案 3 :(得分:0)

根据我对你的问题的理解。可能是以下是你的答案。请检查。

在html中:

<select id="CellLayout" style="color:#99FF00; font-family:monospace;" onChange="PipeConfigChange(this.value);">
    <option>select layout</option>
    <option>blinker</option>
    <option>glider</option>
    <option>flower</option>
    <option>custom</option>
</select>
<input type="button" value="clickHere" onclick="javascript:call()"/>

在javascript中:

function call() {
        var textToFind = 'custom';
        var dd = document.getElementById('CellLayout');
        for (var i = 0; i < dd.options.length; i++) {
            if (dd.options[i].text === textToFind) {
                dd.selectedIndex = i;
                break;
            }
        }
    }