【发布时间】:2021-08-17 07:18:51
【问题描述】:
我已经使用 jquery 动态创建了一个表单,现在我无法提交表单。
$(document).on('click','.answer',function() {
var a = $(this).data('id'); //getter
if ($(this).parent().parent().parent().find('textarea').length == 0) {
$("#answer_form").remove();
$(this).parent().parent().parent().append('<form class="answer_form" id="answer_form"><textarea rows="4" name="answer" class="form-control"></textarea><input type="hidden" name="business_id" readonly value="<?php echo $single_ad->id ?>"/><input type="hidden" name="question_id" id="question_id" readonly value=""/><div class="form-check form-group" style="margin-top: 5px;"><input class="form-check-input" type="checkbox" value="1" id="answer_secret" name="answer_secret"><label class="form-check-label" for="Secret">Secret<span class="secret">(Only Owner of this business can see this question.)</span></label></div><a href="javascript: void(0)" class="btn-sm btn-danger submit_answer">Submit</a></form>');
}
$('#question_id').val(a);
});
这是我提交表单的方式,下面是我尝试保存此表单的方式,非常感谢一些帮助
$(".submit_answer").click(".answer_form", function (e) {
event.preventDefault();
var id = $(this).attr('data-id');
if (id == '') {
var id1 = '';
} else {
var id1 = id;
}
var formData = new FormData($(".answer_form")[0]);
$.ajax({
url: "<?php echo base_url();?>creview/save_answer/"+id1,
type: 'POST',
data: formData,
async: false,
success: function (data) {
var JsonObject= JSON.parse(data);
if (JsonObject.success) {
$.notify({
title: "Complete : ",
message: JsonObject.message,
icon: 'mdi mdi-checkbox-marked-circle'
},{
type: "success",
placement: {
from: "top",
align: "right"
},
});
setTimeout(function() {
window.location.reload();
}, 2000);
} else {
$.notify({
title: "Error : ",
message: JsonObject.message,
icon: 'mdi mdi-alert'
},{
type: "danger",
placement: {
from: "top",
align: "right"
},
});
}
},
cache: false,
contentType: false,
processData: false
});
});
【问题讨论】:
-
请格式化代码条目,以便其他人更轻松地破译您的代码,并尝试将显示的代码限制在最重要的部分。
标签: jquery