【问题标题】:Clearing form issue结算表格问题
【发布时间】:2013-04-09 15:50:08
【问题描述】:

我在使用 Javascript/jQuery 清除表单时遇到问题

这是我的表单的定义方式 - 所有标签都按应有的顺序关闭

<form id="eventpostform" name="eventpostform" method="post" action="post">
    <table cellspacing="5px" style="margin-top: 10px;" width="100%" cellpadding="0" cellspacing="0">
        <tr>
            <td width="180px">Event Name:</td>
            <td width="210px"><a class="param_submit" style="width:86px;padding:6px 5px;margin-left:16px;" alt="" onclick="clearform();">Clear Form</a></td>

我尝试了以下方法,但没有成功:

$('#eventpostform').clearForm();
$('#eventpostform').resetForm();
$('#eventpostform').reset();
document.getElementById("eventpostform").reset();

前2个什么都不做,其他两个显示错误"Uncaught TypeError: Cannot call method 'reset' of null"

【问题讨论】:

    标签: javascript jquery forms reset


    【解决方案1】:

    您可以在表单对象上使用重置方法清除,而不是表单中的控件。您将必须清除单个 html 元素,但它们的方法,例如要清除文本框,您可以将其值设置为 '' 或某个值。

    重置表单中的所有控件

    document.getElementById("eventpostform").reset();
    

    清除个人控制

    document.getElementById("textboxId").value = '';
    

    使用 jQuery

    $('#id').val('');
    

    【讨论】:

    • 长篇大论——但如果它完成了工作......我就加入了!谢谢!
    • $('[name=event_name]').val(""); 是这样做的...对我来说已经足够好了!
    • 很好,你使用属性选择器作为名称而不是 id,干得好。
    【解决方案2】:

    试试这个:

    $('#eventpostform')[0].reset();
    

    【讨论】:

    • 你确定要加载 jQuery 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多