Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery获取表单中所有收音机/复选框的html代码_Javascript_Jquery - Fatal编程技术网

Javascript jquery获取表单中所有收音机/复选框的html代码

Javascript jquery获取表单中所有收音机/复选框的html代码,javascript,jquery,Javascript,Jquery,我试图解析表单的一些元素 我随身带着表格ID/表格名称 现在,我想通过name=“radio123”(或id=“radio123”)解析所有单选按钮。 但是当我在每个元素(单选按钮)上尝试$(this).html时。。。然后我得到一个空白值 如何访问表单中所有单选按钮/复选框的HTML代码?类似的功能应该可以使用- var htmlarr = []; $("input[type='radio'][name='radio123'],[type='radio'][id='radio123']").

我试图解析表单的一些元素

我随身带着表格ID/表格名称

现在,我想通过name=“radio123”(或id=“radio123”)解析所有单选按钮。 但是当我在每个元素(单选按钮)上尝试$(this).html时。。。然后我得到一个空白值


如何访问表单中所有单选按钮/复选框的HTML代码?

类似的功能应该可以使用-

var htmlarr = [];

$("input[type='radio'][name='radio123'],[type='radio'][id='radio123']").each(function () {
   //any logic you need here
   htmlarr.push($(this).clone().wrap('<div>').parent().html());
})
var htmlar=[];
$(“输入[type='radio'][name='radio123'],[type='radio'][id='radio123']])。每个(函数(){
//你需要什么逻辑
htmlarr.push($(this.clone().wrap(“”).parent().html());
})
演示-

代码使用此方法
$(this).clone().wrap(“”).parent().html()
获取DOM元素的外部html。更多信息可在问题中找到-


上面的代码将所有单选按钮html写入一个数组,但您可以在循环中更改它以执行您希望的操作

这是函数的正常行为:
此方法使用浏览器的innerHTML属性。
如果不理解我的意思,请查看示例

我不知道为什么要获取HTML(如果需要值,请查看方法),但这里有一个解决方案

$("input:radio").each(function () {
   console.log( this.outerHTML );
});
要小心使用outerHTML,因为并非所有浏览器都支持它。您可以使用此功能:

function getOuterHTML( node ) {
    var parent = node.parentNode,
        el = document.createElement( parent.tagName ),
        shtml;

    el.appendChild( node );
    shtml = el.innerHTML;
    parent.appendChild( node );

    return shtml;
}
// use it like getOuterHTML( this ) in the preceding each loop

序列化可能对您有所帮助:已回答,请将您的评论/疑问张贴在此处,不要重复question@Vikk-对不起,我忘了我已经把问题贴在那里了…以后会注意的。。。