如何清除变量html内容中的所有字段? (最好用jQuery)
我需要重置保存在变量中的HTML内容中的所有表单字段。 我也在网上搜索过。 但我发现的一切都没有奏效。 即使使用最简单的文本字段方法,它也不会成功:
var content = $(this).prev('.listset').find('ol.multiple > li:last').html(); $(content).find('input[type=text]').val('');
我没有可用的表单ID。
使用普通的旧javascript:
document.forms['my_form_name'].reset()
我找到了这个:
如果您的表单的ID是myform,这应该可以解决问题:
$(':input','#myform') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .removeAttr('selected');
它使用:输入选择器,它将匹配所有输入,textarea,select和button元素。 由于我们将#myform作为第二个参数传递,因此它只会在此表单元素中找到输入。 然后使用not()过滤掉所有按钮,提交,重置和隐藏输入。 然后使用val()将剩余字段的值设置为空字符串,然后使用removeAttr删除字段的已检查和选定属性,以防您有任何radio / checkbox / select输入。 田田。
这里: 使用jQuery重置多阶段表单
编辑:来自相同的url:如果您的复选框上有默认值,请使用以下内容:
// Use a whitelist of fields to minimize unintended side effects. $(':text, :password, :file, SELECT', '#myFormId').val(''); // De-select any checkboxes, radios and drop-down menus $(':input', '#myFormId').removeAttr('checked').removeAttr('selected');
这是你在找什么?
如果您使用的是所有文本框:
for(c in document.getElementById('id_of_form').childNodes) { c.innerHTML = ""; }
如果您正在使用多个内容,请将childNodes替换为getElementByTagName(“tag of tag”)并使用type进行过滤。
这是一个猜测,因为我不知道你的标记是什么样的,但jQuery’find’可能不起作用取决于html字符串的结构 – 看看这个问题 –
使用jQuery搜索HTML字符串
您可以尝试循环遍历’content’字符串,然后单独更新每个表单元素并将它们添加到相关容器中,类似于 –
$(content).each(function () { if (this.nodeName == 'INPUT') $(this).val(''); if (this.nodeName == 'SELECT') $(this).children('option').prop('selected',''); $("#moveto").append(this); });
工作演示 – http://jsfiddle.net/vFMWD/5/