动态创建表单

时间:2013-04-28 13:43:11

标签: jquery forms dynamic

我有这个:

    $('input[data-type="UserDetails"]').each(function(index, input){
        var name = $(input).attr('name');
        var value = $(input).is(":checked");
        alert('name: ' + name + ' value:' + value);
        });

        showForm = "<form><fieldset><legend>Headline here</legend><label>Name</label><input type='text' class='input-fluid' name='txtName'><label class='checkbox'><input type='checkbox'> Accept Terms & Conditions</label><button type='submit' class='btn'>Send</button></fieldset></form>";

首先要做的是检查选中的复选框。并根据我想将它们添加到表单中。就像是“检查名称的文本字段的复选框”。如何将它们添加到“showForm”形式的最明智的方法是什么?

我可以在“showForm”中使用true / false语句吗?在这种情况下如何使用它?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

预先构建表单并为其提供display: none。一旦你有了这个,实现以下功能。

$('input[data-type="UserDetails"]').each(function(index, input){
        var name = $(input).attr('name');
        if($(input).is(":checked")){
            $("form").append('<input type="text" value="' + value + '" />');
        }
        });

 $("form").show(); // Show the form
相关问题