如何清除变量html内容中的所有字段? (最好用jQuery) [英] How do I clear all the fields in a variable html content? (preferably with jQuery)

查看:155
本文介绍了如何清除变量html内容中的所有字段? (最好用jQuery)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要重置保存在变量中的HTML内容中的所有表单字段。我也搜索了互联网。但我发现的一切都没有奏效。即使使用最简单的文本字段方法也不会成功:

I need to reset all the form fields in an HTML content I saved in a variable. I searched the internet and here too. But none of what I've found has worked yet. It won't success even with the simplest method for just text fields:

var content = $(this).prev('.listset').find('ol.multiple > li:last').html();
$(content).find('input[type=text]').val('');

我没有可用的表单ID。

推荐答案

这是一个猜测,因为我不知道你的标记是什么样的,但jQuery'find'可能不起作用取决于html字符串的结构 - 看看这个问题 -

This is a bit of a guess as I don't know what your mark-up looks like but jQuery 'find' may not work depending on how the html string is structured - have a look at this question -

使用jQuery搜索HTML字符串

您可以尝试循环遍历'content'字符串,然后单独更新每个表单元素并将它们添加到相关容器,类似于 -

You could try looping through the 'content' string, then update each form element individually and add them to relevant container, something like -

$(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/

这篇关于如何清除变量html内容中的所有字段? (最好用jQuery)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆