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

求一个js的处理方法。
PHP code
  <script type="text/javascript">
        function CheckInput(obj,type){
            var content = type+'content';
            //alert(content);
            alert(obj.content.value);
            //问题出在这里,怎么获取不到当前表单项的值呢?
            return false;
        }
  </script>

  <form action="save.php" method="post" name="aform" onsubmit="return CheckInput(this,'a')">
      <input type="text" name="acontent" />
      <input type="submit" name="submit" value="发布" />
  </form>
  <form action="save.php" method="post" name="bform_1" onsubmit="return CheckInput(this,'b')">
      <input type="text" name="bcontent" />
      <input type="submit" name="submit" value="发布" />
  </form>
  <form action="save.php" method="post" name="bform_2" onsubmit="return CheckInput(this,'b')">
      <input type="text" name="bcontent" />
      <input type="submit" name="submit" value="发布" />
  </form>





我是想根据参数不同然后获取到不同名称的表单项的值。

目前,我唯一能想到的办法是把里面的acontent和bcontent表单项的name都改成相同的,比如content,然后再通过js函数处理,但我总觉得这个思路有问题。

所以还是比较偏向于能通过js的参数来判断。在js论坛问了一下,貌似也没有好的方法。所以发到这里看看谁能帮忙看下。

------解决方案--------------------
抱歉,发现自己写的有纰漏
function CheckInput(obj, type){
// obj无用
var content = type+'content';
// 这样获取吧
alert(document.getElementsByName(content)[0].value);
return false;
}
------解决方案--------------------
加个同名id
document.getElementById(content).value
------解决方案--------------------
JScript code
<script type="text/javascript">
        function CheckInput(obj,type){
            var content = type+'content';

            alert(obj.content.value); //对象没有这么组合的。应该改为:obj.elements[content].value;
            
            return false;
        }
  </script>

------解决方案--------------------
<script type="text/javascript">
function CheckInput(obj,type){
var content = type+'content';
//alert(content);
alert(obj.content.value); //alert(obj.firstChild.value);这样写可以
//问题出在这里,怎么获取不到当前表单项的值呢?
return false;
}
</script>