【发布时间】:2011-12-21 15:54:51
【问题描述】:
我的 HTML 是:
<table class="tableData" id="addUserTable" >
<tr>
<th class = "" name="errorMsgUser" id="errorMsg" value="" colspan="2"> </th>
</tr>
<tr>
<th class= "" > Desired Username </th>
<td><input name="desUname" type="text" id="desUname" size="30" value="" /></td>
</tr>
<tr>
<th class= "" > Password </th>
<td><input type="password" name="password" id="password" value="" size="30" /></td>
</tr>
<tr>
<th></th>
<td>
<input name="submitAddUser" type="submit" id="submitAddUser" value="Add User" />
<input type="button" value="Clear All" id="clearUpdateForm"></input>
</td>
</tr>
</table>
我制作了一个通用函数,用于清除表单并聚焦第一个输入框
$.clearForm = function() {
var parentEle = $(this).closest('form');
parentEle.find(':input').each(function() {
switch(this.type) {
case 'password':
case 'select-multiple':
case 'select-one':
case 'text':
case 'textarea':
$(this).val('');
break;
case 'checkbox':
case 'radio':
this.checked = false;
}});
parentEle.find('input[type=text]:first').focus();
};
我现在如何在表单上调用此函数?例如,给定上述标记,我可以这样绑定函数吗:
$(function(){$("#clearUpdateForm").click(function(){
$.clearForm (this);
});});
【问题讨论】:
-
'减号' ...我真的不明白你的问题。
-
您的函数名为 clearForm,但您正在调用 resetForm。错字?
-
标题仍然毫无意义,请更改它以反映您的实际问题..