[英]Submitting a dynamically created form in jQuery
I am trying to submit a form that I dynamically created.我正在尝试提交一个我动态创建的表单。 I am using the .submit, but it is not working.
我正在使用 .submit,但它不起作用。 Here is my code
这是我的代码
var $message = $('.message');
$message.submit(function(e){
console.log("Test");
});
$(document.body).on('click','.button', function(e) {
console.log($(this).val());
var $input = '';
$input = $('<p>Chat with '+$(this).val()+'</p>');
$input.append('<div class = "chat" style="height:200px"></div>');
$input.append('<form class="message">');
$input.append('<input size = "35" class="messages">');
$input.append("<br><input type='submit' class='send_message' value = 'Send' />");
$input.append('</form>');
$input.appendTo($("#contentWrap"));
});
The code for document.body created the form. document.body 的代码创建了表单。 However, when I click the submit button nothing happens.
但是,当我单击提交按钮时,没有任何反应。
I also tried我也试过
$('#contentWrap').on("submit", ".message", function(e){
console.log("Test")
});
$(parent).on(event, child, function) is what most of the posts I read say to do. $(parent).on(event, child, function) 是我读过的大多数帖子都说要做的。 However, I must be doing something else wrong.
但是,我一定是做错了什么。
Here is the fill front end code in js fiddle https://jsfiddle.net/55Ln0wgu/这是js小提琴https://jsfiddle.net/55Ln0wgu/中的填充前端代码
The problem is that the submit
button isn't in the form.问题是
submit
按钮不在表单中。 You're appending each <input>
element to the <p>
, not the <form>
.您将每个
<input>
元素附加到<p>
,而不是<form>
。 The result is equivalent to this HTML, which has an empty <form>
.结果等同于这个 HTML,它有一个空的
<form>
。
<p>Chat with
<div class="chat" style="height:200px"></div>
<form class="message"></form>
<input size="35" class="messages"><br>
<input type="submit" class="send_message" value="Send">
</p>
Remember, .append()
operates on the DOM hierarchy, it's not concatenating strings to the HTML.请记住,
.append()
对 DOM 层次结构进行操作,它不是将字符串连接到 HTML。
Your code would work if you built $input
as a string.如果您将
$input
构建为字符串,您的代码将起作用。
You also need to use event delegation for the Submit button, since it's being added dynamically.您还需要为 Submit 按钮使用事件委托,因为它是动态添加的。
$(document.body).on('click', '.button', function(e) { console.log($(this).val()); var $input = '<p>Chat with ' + $(this).val(); $input += '<div class = "chat" style="height:200px"></div>'; $input += '<form class="message">'; $input += '<input size = "35" class="messages">'; $input += "<br><input type='submit' class='send_message' value = 'Send' />"; $input += '</form></p>'; $("#contentWrap").append($input); }); $('#contentWrap').on("submit", ".message", function(e) { alert("Test"); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> Form should appear here: <div id="contentWrap"> </div> Chat with: <input type="button" class="button" value="Fred"> <input type="button" class="button" value="Joe">
Try this尝试这个
$('#contentWrap').on('click', '.button', function(e) {
console.log($(this).val()); `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.