将Javascript函数参数转换为jQuery或HTML标记

将Javascript函数参数转换为jQuery或HTML标记,javascript,jquery,html,Javascript,Jquery,Html,这是我创建输入字段的原始代码: $('#dynamicForm').append('<li id="pdport"></li>'); $('#pdport').append("<label for='sport'>Destination port</label>") $('<input>').attr({type: 'text', id: 'dport', name: 'dport'}).appendTo('#pdport'); $('

这是我创建输入字段的原始代码:

$('#dynamicForm').append('<li id="pdport"></li>');
$('#pdport').append("<label for='sport'>Destination port</label>")
$('<input>').attr({type: 'text', id: 'dport', name: 'dport'}).appendTo('#pdport');
$('#pdport').append("<span>Enter the destination port here (0-65535)</span>");
通过这种方式,我可以创建大量动态输入字段,而无需复制/粘贴大量相同的代码


问题是这不起作用,我总是会遇到语法错误,比如你不能附加到mainID和其他。我尝试了很多不同的方法来实现这一点,比如使用引号,使用单引号,不使用。。但似乎什么都不管用

您忘记了对变量进行scape,并将它们关联到字符串

function createInputfield(mainID, childID, labelFor, labelText, spanText){
    $(mainID).append('<li id='+childID.replace('#','')+'></li>');
    $(childID).append("<label for="+labelFor+">"+labelText+"</label>")
    $('<input>').attr({
        type: 'text', 
        id: labelFor, 
        name: labelFor
    }).appendTo(childID);
    $(childID).append("<span>"+ spanText +"</span>");
}
函数createInputfield(mainID、childID、labelFor、labelText、spanText){
$(mainID).append('
  • '); $(childID).append(“+labelText+”) $('').attr({ 键入:“文本”, id:labelFor, 姓名:拉贝弗 }).appendTo(childID); $(childID).append(“+spanText+”); }

    这可能会起作用

    您忘记了对变量进行scape,并将它们关联到字符串

    function createInputfield(mainID, childID, labelFor, labelText, spanText){
        $(mainID).append('<li id='+childID.replace('#','')+'></li>');
        $(childID).append("<label for="+labelFor+">"+labelText+"</label>")
        $('<input>').attr({
            type: 'text', 
            id: labelFor, 
            name: labelFor
        }).appendTo(childID);
        $(childID).append("<span>"+ spanText +"</span>");
    }
    
    函数createInputfield(mainID、childID、labelFor、labelText、spanText){
    $(mainID).append('
  • '); $(childID).append(“+labelText+”) $('').attr({ 键入:“文本”, id:labelFor, 姓名:拉贝弗 }).appendTo(childID); $(childID).append(“+spanText+”); }

    这可能行得通

    马文就快到了。他发现缺少字符串连接。您还忘了您正在发送以
    #
    开头的字符串,但正在使用无法接受以
    #
    开头的字符串的字符串

    更新:看起来马文也注意到了
    的用法。他选择剥掉它。我的解决方案在需要的地方添加散列,因此您不必担心将散列传递到函数中

    JS:

    function createInputfield(mainID, childID, labelFor, labelText, spanText){
        $('#'+ mainID).append('<li id=' + childID + '></li>');
        $('#'+ childID).append('<label for=' + labelFor + '>' + labelText + '</label>')
        $('<input>').attr({type: 'text', id: labelFor, name: labelFor}).appendTo('#'+ childID);
        $('#'+ childID).append('<span>' + spanText + '</span>');
    }
    
    createInputfield("dynamicForm", "psport", "sport", "Source port", "Enter the source port here (0-65535)");
    
    函数createInputfield(mainID、childID、labelFor、labelText、spanText){
    $('#'+mainID).append('
  • '); $('#'+childID).append(''+labelText+'') $('').attr({type:'text',id:labelFor,name:labelFor}).appendTo('#'+childID); $('#'+childID).append(''+spanText+''); } createInputfield(“dynamicForm”、“psport”、“sport”、“源端口”、“在此处输入源端口(0-65535)”);

    演示:

    马文快到了。他发现缺少字符串连接。您还忘了您正在发送以
    #
    开头的字符串,但正在使用无法接受以
    #
    开头的字符串的字符串

    更新:看起来马文也注意到了
    的用法。他选择剥掉它。我的解决方案在需要的地方添加散列,因此您不必担心将散列传递到函数中

    JS:

    function createInputfield(mainID, childID, labelFor, labelText, spanText){
        $('#'+ mainID).append('<li id=' + childID + '></li>');
        $('#'+ childID).append('<label for=' + labelFor + '>' + labelText + '</label>')
        $('<input>').attr({type: 'text', id: labelFor, name: labelFor}).appendTo('#'+ childID);
        $('#'+ childID).append('<span>' + spanText + '</span>');
    }
    
    createInputfield("dynamicForm", "psport", "sport", "Source port", "Enter the source port here (0-65535)");
    
    函数createInputfield(mainID、childID、labelFor、labelText、spanText){
    $('#'+mainID).append('
  • '); $('#'+childID).append(''+labelText+'') $('').attr({type:'text',id:labelFor,name:labelFor}).appendTo('#'+childID); $('#'+childID).append(''+spanText+''); } createInputfield(“dynamicForm”、“psport”、“sport”、“源端口”、“在此处输入源端口(0-65535)”);

    演示:

    拍摄一张照片,这是我的代码,希望像它一样,我在数组中找到对象,然后把它们放在一起

    function createInputfield(mainID, childID, labelFor, labelText, spanText){
    
    
        var elms = [
            $('<label for=' + labelFor + ' />').text(labelText),
            $('<input />').attr({type: 'text', id: labelFor, name: labelFor}),
            $('<span />').text(spanText)
        ];
        $('<li id=' + childID + '></li>').append(elms).appendTo('#'+ mainID)
    
    }
    
    createInputfield("dynamicForm", "psport", "sport", "Source port", "Enter the source port here (0-65535)");
    
    函数createInputfield(mainID、childID、labelFor、labelText、spanText){
    var elms=[
    $('').text(labelText),
    $('').attr({type:'text',id:labelFor,name:labelFor}),
    $('').text(spanText)
    ];
    $('
  • ).append(elms).appendTo('#'+mainID) } createInputfield(“dynamicForm”、“psport”、“sport”、“源端口”、“在此处输入源端口(0-65535)”);

    希望你喜欢它

    拍摄一张照片,这是我的代码,希望你喜欢,我在数组中找到对象,然后把它们放在一起

    function createInputfield(mainID, childID, labelFor, labelText, spanText){
    
    
        var elms = [
            $('<label for=' + labelFor + ' />').text(labelText),
            $('<input />').attr({type: 'text', id: labelFor, name: labelFor}),
            $('<span />').text(spanText)
        ];
        $('<li id=' + childID + '></li>').append(elms).appendTo('#'+ mainID)
    
    }
    
    createInputfield("dynamicForm", "psport", "sport", "Source port", "Enter the source port here (0-65535)");
    
    函数createInputfield(mainID、childID、labelFor、labelText、spanText){
    var elms=[
    $('').text(labelText),
    $('').attr({type:'text',id:labelFor,name:labelFor}),
    $('').text(spanText)
    ];
    $('
  • ).append(elms).appendTo('#'+mainID) } createInputfield(“dynamicForm”、“psport”、“sport”、“源端口”、“在此处输入源端口(0-65535)”);

    希望您喜欢它

    他在字符串中发送id选择器,我刚刚用.replace方法删除了散列。他在字符串中发送id选择器,我刚刚用.replace方法删除了散列