+ -
当前位置:首页 → 问答吧 → 请问jquery如何实现form的reset?

请问jquery如何实现form的reset?

时间:2009-04-12

来源:互联网

我用了这两个:
$("form:first")[0].reset;

document.getElementById("form1").reset;

但都不起作用。

作者: benlaohu   发布时间: 2009-04-12

reset()

作者: shawphy   发布时间: 2009-04-12

如果加上括号,那么firebug就会报错,说没有这个方法。。。

作者: benlaohu   发布时间: 2009-04-12

复制代码
  1. <form method="post" action="" id="form1">
  2.     <input type="text" value="xxx"/>
  3. </form>
  4. <button>重置1</button><button>重置2</button>
  5. <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  6. <script type="text/javascript">
  7. $("button").eq(0).click(function(){
  8.     $("form:first")[0].reset();
  9. });
  10. $("button").eq(1).click(function(){
  11.     document.getElementById("form1").reset();
  12. });
  13. </script>

只能帮到这里了,剩下的靠你自己咯~加油吧~

作者: shawphy   发布时间: 2009-04-12

多谢。不过还是不行,奇怪。

    $("#checkandget").click(function() {
        $("form:first")[0].reset();
        getResponseXML();
    })

checkandget是按钮。我如果把$("form:first")[0].reset();注释掉,代码可以执行;如果不注释,那么会停留在这一句,说没有这个方法。

作者: benlaohu   发布时间: 2009-04-12

你可以保存并运行一下我给你的代码,
我那个代码我是测试过再发上来的。
然后对比一下你的代码中哪出了问题。

作者: shawphy   发布时间: 2009-04-12

多谢斑竹答疑。你的代码我试了,没有问题,但很奇怪,我的就是不行。下面是我的代码,里面有部分删掉了,但跟这个reset无关。

<input name="checkandget" type="button" id="checkandget" value="test ..." />
//上面这一行是我的按钮

<input type="submit" name="save" id="save" value="提交" />
<input type="reset" name="reset" id="reset" value="重置" />
这两行是form的按钮定义

下面的脚本我是写在form后面的。

<script type="text/javascript">
    function getResponseXML() {
        $.ajax({
            url: ".Func.php",  //请求的地址
            type: "GET",
            data: "act=xxx",  //这是我的参数
            dataType: "xml",
            error: function(xml) {
                alert('Error loading XML doc' + xml);
            },
            success: function(xml) {
                // reset form first
                $("form:first")[0].reset;  //如果reset(),firebug就报告没有这个方法。
                //document.getElementById("form1").reset;
                
                // for radio elements
                $(":radio").each(function() {
                    $(this).removeAttr("checked")
                })
            }
        })
    }
    
    $("#checkandget").click(function() {
        $("form:first")[0].reset;
        getResponseXML();
    })
</script>                

作者: benlaohu   发布时间: 2009-04-12

推测是因为你的form中有个id为reset的元素,所以你那样写的话,浏览器就认为你找的是那个元素了,自然就执行失败了
你试着改一下那个id

作者: shawphy   发布时间: 2009-04-13

搞定!就是因为那个id是reset的元素!!

多谢多谢。。。

作者: benlaohu   发布时间: 2009-04-13

.reset();呀

作者: abee   发布时间: 2010-10-02