通过jquery添加表单

时间:2015-01-28 03:33:44

标签: javascript jquery html forms

以下代码是通过jquery向我的页面添加表单。但它没有用。有人可以帮我找到这个bug吗?这是代码:

var form ='<form class="form-inline"><div class="control-group"><input autofocus class="form-control" placeholder="food name" type="text"/></div><div class="control-group"><select name="type" class="form-control"><option value="deluxe">Deluxe</option><option value="classic">Classic</option><option value="americana">Americana</option><option value="beverage">Beverage</option><option value="protein">Protein</option></select></div><div class="control-group"><select name="category" class="form-control"><option value="1">Breakfast</option><option value="2">Lunch & Supper</option><option value="3">Lunch & Supper</option></select></div><div class="control-group"><input name="price" class="form-control" placeholder="Price" type="text"/></div><div class="control-group"><button type="submit" class="btn btn-default">Submit</button></div></form>';

//add form to html
function addForm(){
    $(".added").append(form);
}

$(document).ready(function(e){
    e.preventDefault();
    $("a[href*=#]").click(addForm);
});

1 个答案:

答案 0 :(得分:0)

试试这个:

function addForm(form){
    $(".added").append(form);
}

$(document).ready(function(){
    var form ='<form class="form-inline"><div class="control-group"><input autofocus class="form-control" placeholder="food name" type="text"/></div><div class="control-group"><select name="type" class="form-control"><option value="deluxe">Deluxe</option><option value="classic">Classic</option><option value="americana">Americana</option><option value="beverage">Beverage</option><option value="protein">Protein</option></select></div><div class="control-group"><select name="category" class="form-control"><option value="1">Breakfast</option><option value="2">Lunch & Supper</option><option value="3">Lunch & Supper</option></select></div><div class="control-group"><input name="price" class="form-control" placeholder="Price" type="text"/></div><div class="control-group"><button type="submit" class="btn btn-default">Submit</button></div></form>';
    $(this).on('click', 'a[href*=#]', function(e) {
        e.preventDefault();
        addForm(form);
    });;
});