使用带表单的Reset()函数返回“未定义的属性”

问题描述 投票:3回答:2

我试图使用Reset()函数重置我的表单,但我收到如下错误:

Uncaught TypeError: Cannot read property 'reset' of undefined

虽然它没有任何问题!

这是我的代码(jQuery):

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

(HTML):

<form method="POST" action="https://petrocanada.dev/admin/coding/vendors" accept-charset="UTF-8" id="frmManageCoding">
    <input name="_token" type="hidden" value="soXhq24ixsvzhRKPDBwcSgeyxv3BiqIW5LJWhjCX">
    <input type="hidden" name="_method" value="POST">
    <input id="id" name="id" type="hidden">
    <input id="page" name="page" type="hidden" value="vendors">
    <div class="modal-header">
        <h5 class="modal-title" id="manageCodingModalLabel">Add New</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
            <span aria-hidden="true">&times;</span>
        </button>
    </div>
    <div class="modal-body">
        <div class="form-group">
            <label for="name">Name</label>
            <input class="form-control" placeholder="Enter brand name" required="required" name="name" type="text" id="name">
        </div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" class="btn btn-primary">Save changes</button>
    </div>
</form>

有什么建议?

javascript jquery forms undefined reset
2个回答
1
投票

如果您使用的是ID,则不需要定位数组中的特定项([0]表示它是第一项)。试试这个(未经测试但很容易尝试)

$('#frmManageCoding').reset();

希望它有效,祝你好运:)


1
投票

修好了。

我以错误的方式使用appendTo函数,这导致表单在使用Reset()函数之前为空,这会导致该错误。

感谢@Barmar帮我解决了这个问题。

© www.soinside.com 2019 - 2024. All rights reserved.