html代码
  <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>问题是我这样获取不到所需要的值,js部分应该怎么写?

解决方案 »

  1.   

    直接用document.表单名.里面的控件你试试 应该可以
      

  2.   

    obj.content.value 写法有问题。。虽然上面看content是一个参数但是下面去把它当成了表单的名字。。
    document.aform.acontent.value这样是可以得到的。。
      

  3.   

    <script type="text/javascript">
    function submitForm(f){
    var form = f;
    alert(form.textbox.value);
    }
    </script>
    <form onsubmit="submitForm(this)">
    <input type="text" name="textbox" value="123" />
    <input type="submit" value="提交" />
    </form>不知道你要实现什么功能 但这样写可以
      

  4.   

    alert(obj.content.value);
    你这句话有问题 obj.content 不能点出来 因为表单里没有这个名字的控件
    你的content是上面定义的 变量  怎么能在表单对象里点出你自定义的变量呢?
      

  5.   

    alert(obj.acontent.value);
    你应该这么写 才能获取到值
      

  6.   

    是的,我是想通过后面的type参数来获取指定表单的值的。
      

  7.   

    alert(obj.content.value);
    content  这个元素名用变量代替行吗?在这里这种情况可能js会把content直接当做元素名去查找了吧!你这样试试呢:alert(obj.(+content+).value);
    若不行的话你还是就用if去判读吧
      

  8.   

    看来只能通过html这里来处理了,只要把input的name都改成content就可以调用了。js貌似是没好办法。
      

  9.   

    为什么不加个id使用
    document.getElementById(content).value
    使用jquery更简单
    $('#'+content).val()
      

  10.   

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

  11.   

    <script type="text/javascript">
            function CheckInput(obj,type){
                var content = type+'content';            alert(obj.content.value); //对象没有这么组合的。应该改为:obj.elements[content].value;
                
                return false;
            }
      </script>