我们经常会在页面上用到radio形式的选项表单,那么我们如何判断当前哪个radio被选中了呢?首先我们做如下的一个函数,它将返回当前被选中的radio选项的值: <script><!-- function get_radio_value (radio_array) { var i; for (i = 0; i < radio_array . length; ++ i) //radio_array.length是radio选项的个数 if (radio_array [i] . checked) return radio_array [i] . value; return null; //如果一项都没选则返回空值 } //--></script> 接着,我们通过以下的表单脚本来调用以上函数: <form> What is your favorite fruit?<br> <input type=radio name=fruit value=apple>apple<br> <input type=radio name=fruit value=orange>orange<br> <input type=radio name=fruit value=banana>banana<br> <input type=radio name=fruit value=other>other <input type=text name=echotext> <input type=button value=echo onClick="form . echotext . value = get_radio_value (form . fruit);"> <input type=reset value=clear> </form> 当点击"echo" 按扭时,被选中的radio项的值就会显示在echotext框中。 欢迎咨询/
|