使用 jQuery 从 html-string 获取表单的所有输入 - 无论它们是哪个级别

Get all input of form from a html-string with jQuery - No matter which level they are

我想从 html 字符串中提取所有表单及其输入字段。为此,我编写了以下代码:

var html = $.parseHTML("<html><form id="Form1"><div><input name="Input1"></div></form></html>");
$(html).find('form').each(function(e) {
    var FormElement = $(this);

    $(FormElement).find('input, textarea').each(function(e) {
        consolge.log($(this).attr('name') + " is an input field of a form with the the ID: " + $(FormElement).attr('id'));
    });
});

遗憾的是,这只会提取表单的子输入字段。表单中的 div 中的输入字段不受扫描影响。

(工作示例:html -> 表单 -> 输入)

(无效示例:html -> 表单 -> div -> 输入)

有什么建议吗?

提前致谢! :)

我已更新您的代码以使用 (html).each()

//with multipule forms
var html = jQuery.parseHTML("<html><form id='Form1'><div><input name='Input1'></div></form><form id='Form2'><div><input name='Input2'></div></form></html>");

jQuery(html).each(function(e) {

    var id = jQuery(this).attr('id');
    jQuery(this).find('input, textarea').each(function(e) {
        //that will output for every input and textarea
        console.log(jQuery(this).attr('name') + " is an input field of a form with the the ID: " + id );

    });
});