日期:2014-05-16  浏览次数:20334 次

js页面元素取值问题
页面为:
<body>
        <form   name= "testFomr "   method= "post ">
                <table>
                    <tr>
                              <td>
                                      <input   type= "chekcBox "   name= "checkBox_0 "   value= "0 "   onclick= "showValue( 'checkBox_0 ') ">
                              </td>
                    </tr>
                    <tr>
                              <td>
                                      <input   type= "chekcBox "   name= "checkBox_1 "   value= "1 "   onclick= "showValue( 'checkBox_1) ">
                              </td>
                    </tr>
                    <tr>
                              <td>
                                      <input   type= "chekcBox "   name= "checkBox_2 "   value= "2 "   onclick= "showValue( 'checkBox_2) ">
                              </td>
                    </tr>
                </table>
        </form>
</body>


js函数:
function   showValue(itemName){
        var   fm   =   document.forms[0];
        var   item   =   "fm. "   +   itemName;
        var   item1   =   "document.forms[0].checkBox_0 ";
        alert(item.value);
        alert(item1.value);
}


问题:
        js函数alert()的时候总是提示 "undefined ",请问这是咋回事?如果我想依然用这种传元素名称的方式取值,应该怎么做?谢谢!

------解决方案--------------------
showValue(this)
------解决方案--------------------
function showValue(itemName){
var fm = document.forms[0];
var item = eval( "fm. " + itemName);
var item1 = eval( "document.forms[0].checkBox_0 ");
alert(item.value);
alert(item1.value);
}