[英]Getting back object Object from validation using jquery ajax
In my jquery function I set up some validation so that if a user doesnt enter any data into these fields, it sends back a message saying "please enter + whatever field they missed".在我的 jquery function 中,我设置了一些验证,以便如果用户没有在这些字段中输入任何数据,它会发回一条消息,说“请输入+他们错过的任何字段”。 However when I test this in my application, the messages I get back say object Object instead of the text that I push into the empty errors array.
但是,当我在我的应用程序中对此进行测试时,我得到的消息是 object Object 而不是我推入空错误数组的文本。 Any advice on how to fix this is greatly appreciated!
非常感谢任何有关如何解决此问题的建议! Thanks!
谢谢!
$('#formData').on('click', 'button.addRow', function (e) {
const cloneRow = $('#tableData tbody tr').first();
e.preventDefault();
let errors = [];
let data = {
project_id: $(".project_id").last().val(),
imp_or_ann: $(".imp_or_ann").last().val(),
category: $(".category").last().val(),
cost: $(".cost").last().val(),
hours: $(".hours").last().val()
}
if (!data.project_id) {
errors.push({text: "please enter a project id"})
}
if (!data.imp_or_ann) {
errors.push({text: "please select an option"})
}
if (!data.category) {
errors.push({text: "please select an option"})
}
if (!data.cost) {
errors.push({text: "please enter a value for cost"})
}
if (!data.hours) {
errors.push({text: "please enter a value for hours"})
if (errors.length > 0){
for (var item in errors) {
$("#errors").append("<p style=\"border: 1px solid black;\">" + errors[item] + '</p>')
}
}
} else {
$.ajax({
url: '/costs_hours',
type: 'POST',
data: data
}).then(
cloneRow.clone().appendTo('#tableData tbody').find(".cost, .hours").val(''),
$("#next").removeAttr('disabled'),
$("#link").attr('href', '/fundings'),
console.log(data)
)
}
})
$("#errors").append("<p style=\"border: 1px solid black;\">" + errors[item] + '</p>')
You are concatenating object try this您正在连接 object 试试这个
errors[item].text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.