在jQuery中动态添加按钮

时间:2017-10-21 10:31:36

标签: jquery html

您好我已经在jQuery中动态创建了一个表单。我想要做的是在表单中的标签左侧添加按钮(如列)。我尝试过append(),insertbefore(),prepend()但是它们没有提供我需要的结果。我附上了一张图片来表达这个想法。There are labels of the form shown and then after the labels are their respective inputs. The buttons are to be place before the labels on the left。谢谢:))

//The form in html:

<form>
  <input type="text" name="firstname">
  <br>
  <input type="text" name="lastname">
</form>

// the jQuery to dynamically create input button

var text_input = $('<button/>', {
  text: "Text Input",
  id: 'btn_',
  click: function () {
    var original_form = $('form').last();
    var template = $('form input').first();
    var new_input = template.clone();
    new_input.insertAfter(original_form);
  }
});

$('form').prepend(text_input);  // everything works except this last line where I want this button to be on the left of the input of the form

0 个答案:

没有答案