Javascript 对表单使用Reset()函数返回;“未定义”的属性;
我试图使用Reset()函数重置我的表单,但出现如下错误:Javascript 对表单使用Reset()函数返回;“未定义”的属性;,javascript,jquery,forms,undefined,reset,Javascript,Jquery,Forms,Undefined,Reset,我试图使用Reset()函数重置我的表单,但出现如下错误: Uncaught TypeError: Cannot read property 'reset' of undefined 当它工作时没有任何问题 以下是我的代码(jQuery): (HTML): 新增 &时代; 名称 接近 保存更改 有什么建议吗?如果您使用的是一个ID,那么您不需要针对数组中的特定项([0]表示它是第一项)。试试这个(未经测试但很容易尝试) 希望它能工作,祝你好运:)修好了 我使用appendTo函数的方式错误
Uncaught TypeError: Cannot read property 'reset' of undefined
当它工作时没有任何问题
以下是我的代码(jQuery):
(HTML):
新增
&时代;
名称
接近
保存更改
有什么建议吗?如果您使用的是一个ID,那么您不需要针对数组中的特定项([0]表示它是第一项)。试试这个(未经测试但很容易尝试) 希望它能工作,祝你好运:)修好了 我使用appendTo函数的方式错误,这导致表单在使用Reset()函数之前为空,从而导致该错误
感谢@Barmar帮助我解决了这个问题。你能添加相关的表单代码吗?你从哪里打电话来的?可能您在创建DOM元素之前调用它。可能答案是:@Barmar,我是从一个链接到带有onclick事件的按钮的函数调用它的。可能是实际代码中的一个输入错误,您没有复制到这里或小提琴上。
$('#frmManageCoding')[0].reset();
<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">×</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>
$('#frmManageCoding').reset();