【问题标题】:I have created a form dynamically using jquery, now I am having trouble in submitting the form, any solution for this would be highly appreciated我已经使用 jquery 动态创建了一个表单,现在我在提交表单时遇到了麻烦,对此的任何解决方案将不胜感激
【发布时间】: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


【解决方案1】:

我认为您的语法是正确的,但我对您传递的数据有疑问...

最好通过选择每个输入类型来生成数据模型并创建一个结构来传递它们。就像下面的link.

模型样本可以是这样的:

var modelToPost = {
               
                OfficeId: officeId,
                Title: "Title...",
                DayCount: 0,
                BoardingType: "boartdingtype",
                Comment: "...Comment",
                ItemsList: "Items...."
            };

【讨论】:

    猜你喜欢
    • 2020-10-05
    • 2020-06-12
    • 2022-01-03
    • 2019-10-21
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多