radio默认值

Html代码
<html:radio property="consumptionClass" value="花了" >花了</html:radio>  
<html:radio property="consumptionClass" value="赚了" >赚了</html:radio>  
<html:radio property="consumptionClass" value="借出" >借出</html:radio>  
<html:radio property="consumptionClass" value="借入" >借入</html:radio>  

 

方法1

在jsp页面最后面加入

Js代码 
<script>     
  
alert("${consumptionClass}");   
  
if("${consumptionClass}" == "花了")   
{   
    document.all("consumptionClass")[0].checked=true;//第一个radio选中   
}     
if("${consumptionClass}" == "赚了")   
{   
    document.all("consumptionClass")[1].checked=true;//第二个radio选中   
}   
if("${consumptionClass}" == "借出")   
{   
    document.all("consumptionClass")[2].checked=true;//第三个radio选中   
}   
if("${consumptionClass}" == "借入")   
{   
    document.all("consumptionClass")[3].checked=true;//第四个radio选中   
}   
</script>   

 



方法2

要让<html:radio>选择很关键的一点,就是,这个JSP势必对应的一个actionform吧,给它个初始值就OK了

默认值是动态的话

请注意到name属性

性别

Html代码
<html:radio value="花了" property="consumptionClass" name="cco">花了</html:radio>     
<html:radio value="赚了" property="consumptionClass" name="cco">赚了</html:radio>     
<html:radio value="借出" property="consumptionClass" name="cco">借出</html:radio>     
<html:radio value="借入" property="consumptionClass" name="cco">借入</html:radio>    

 

只要在request,session等范围内存在user,这个上面的标签就会根据consumptionClass.cco的值产生默认选中了

<html>  
    <head>  
        <title>初始化单选</title>  
        <script>  
            function initradio(rName,rValue){  
                var rObj = document.getElementsByName(rName);  
 
                for(var i = 0;i < rObj.length;i++){  
                    if(rObj[i].value == rValue){  
                        rObj[i].checked =  'checked';  
                    }  
                }  
            }  
        </script>  
    </head>  
    <body>  
        <form action="#"><input type="radio" name="tRadio" value="1"><input type="radio" name="tRadio" value="2"><input type="radio" name="tRadio" value="3"><input type="radio" name="tRadio" value="4">  
        </form>  
        <script>  
            initradio('tRadio',3);  
        </script>  
    </body>  
</html> 

 

<%@page pageEncoding="gbk"%>
<html>
    <head>
        <title>Index</title>
        <script type="text/javascript">
            function initradio(name,value){
                var r1=document.getElementsByName(name);
                for(var i=0;i<r1.length;i++){
                    if(r1[i].value==value){
                        r1[i].checked=true;
                    }
                }
            }
        </script>
    </head>
    <body onload="initradio('r','<%=request.getParameter("value") %>')">
        <center>
            <h1>Index</h1>
            <hr>
            <p>请选择:</p>
            <input type="radio" name="r" value="A">A
            <br>
            <input type="radio" name="r" value="B">B
            <br>
            <input type="radio" name="r" value="C">C
            <br>
            <input type="radio" name="r" value="D">D
            <br>
        </center>
    </body>
</html>

 


 

 

posted @ 2011-10-07 21:18  残星  阅读(5103)  评论(1编辑  收藏  举报