首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

select上拉列表!使其不可改变!并且可以得到默认值

2013-01-26 
select下拉列表!使其不可改变!并且可以得到默认值用JS代码disabled不行啊!用disabled获得不到默认值求解答

select下拉列表!使其不可改变!并且可以得到默认值
用JS代码
disabled不行啊!用disabled获得不到默认值
求解答啊!项目赶时间啊!
[解决办法]

<script>
function foo(obj){
      obj.selectedIndex=0;
}
</script>
<form action='f.php' method='post'>
<select name='key' id='key' onchange='foo(this)'>
<option value=0>All</option>
<option value=1>aa</option>
<option value=2>bb</option>
<option value=3>cc</option>
</select>
<input type='submit'>
</form>

或者就用disabled禁用掉,将select框的默认值放入一个隐藏域中,再提交。

[解决办法]
<select name='key' id='key' disabled="disabled"> 
<option value=0>All</option> 
<option value=1>aa</option> 
<option value="2" selected="selected">bb</option> 
<option value=3>cc</option> 
</select> 
<script type="text/javascript">
alert(document.getElementById('key').value)
</script>


这样会取不到值????
[解决办法]
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js" type="text/javascript"></script>
    <script type="text/javascript">
        function getValue(){
            alert("第1个select的值:"+document.getElementById('select1').value);
            alert("第2个select的值:"+document.getElementById('select2').value);
        }
    </script>
</head>
<body>
<div>
    <div style="border:solid 1px green;" >
        第1种:按原意,不过客户可能还是会有点误会的(能够动,为什么修改不了?)<br />
        <select id="select1" onfocus="this.defaultIndex=this.selectedIndex;" onchange="this.selectedIndex=this.defaultIndex;">  
            <option value="1">test1</option>  
            <option value="2">test2</option>  
            <option value="3" selected="selected">test3</option>  
            <option value="4">test4</option>  
            <option value="5">test5</option>  
        </select>  
    </div>
    <div style="border:solid 1px red;" >
        第2种:在加载完span和select时,将select2的值给span<br />
        <span id="spanSelect2" ></span>


        <select id="select2" style="display:none;" >  
            <option value="1">test1</option>  
            <option value="2">test2</option>  
            <option value="3" selected="selected">test3</option>  
            <option value="4">test4</option>  
            <option value="5">test5</option>  
        </select>
        <script type="text/javascript">
            //在加载完span和select时,将select2的值给span
            var select2 = document.getElementById("select2");
            var txt = select2.options[select2.selectedIndex].text;
            document.getElementById("spanSelect2").innerHTML = txt;
        </script>
    </div>
    <input type="button" onclick="getValue()" value="取得值" />
</div>
</body>
</html>



两种方法,看着办吧。
[解决办法]

[解决办法]
修改DOM结构行不行。
给select分配个ID,根据其它界面传来的值在body onLoad事件时动态Remove select的Child,然后把仅剩的Child设定selected属性,这样用户页面的下拉框里就只有一个值了。

热点排行