[英]Always returns a undefined value
我正在使用jQuery将Ajax请求发送到服务器,以将输入的值保存在表单中。 在我被卡住的部分下面。 HTML是
<span class="no-margin multiple Date_Off" style="margin-left: 104px;">
<input type="text" value="" /><input type="text" />
<input type="text" value="-" /><input type="text" />
<input type="text" /><input type="text" value="-" />
<input type="text" /><input type="text" /><input type="text" />
<input type="text" />
</span>
我尝试使用jQuery发送请求。 我想做的是这样的
我想将表单中的值保存到输入字段具有的相同Column_Name中。 在多个输入字段中,我没有使用输入名称。 相反,我使用的类名与数据库中的Column_Name相同。
为此,我正在使用$(this).parent().attr('class');
。 如果我在警报中使用它,它会为我提供没有错误的结果。 但是,如果我在代码中使用它,它将给我undefined
。
我想将每个输入的值附加到字符串以将其保存为单个字符串。
这是我到目前为止尝试过的
var input = $('input');
input.change(function () {
// Input click function...
if ($(this).parent().attr('class')
.replace(' multiple ', '')
.replace('no-margins', '') == 'Date_Off') {
// Date Time for the office work!
var value = '';
value = $(this).parent().find('input').each(function (index, ele) {
value += ele.val();
});
send_request('Date_Off', value);
// Below is the else condition, to execute only when the input is single
// Like a single string input and not like the one in image
// That's why I am using attr('name') for that.
} else {
send_request($(this).attr('name'), $(this).val());
}
});
但是它返回的内容在Query结构中始终是未定义的。 这是该功能
function send_request(input_name, value) {
$.ajax({
url: '/ajax_requests/save_form',
data: 'input_name=' + input_name + '&form_id=' +
$('input[type=hidden]').val() + '&value=' + value,
error: function () {
$('.main-content').append(
'There was an error in request.
Please contact your website Developer to fix it.'
);
},
success: function () {
}
});
}
重点输入为1. Date
。 并且控制台显示内部服务器错误。 因为jQuery使用input_name=undefined
而不是Column_Name发送了请求。
我为此创建了一个(迷你)小提琴: http : //jsfiddle.net/afzaal_ahmad_zeeshan/EHWqB/
这里有什么帮助吗?
在此行中,您尝试获取输入的名称
send_request($(this).attr('name'), $(this).val());
我在代码的任何地方都没有看到“名称”属性,我想您想要的是获取父类吗?
send_request($(this).parent().attr('class'), $(this).val());
对于您发布的小提琴,有两个错误。 首先是您正在调用ele.val()
,但是在这种情况下ele
不是jQuery对象-因此您需要从中获取value
属性。 第二个是jQuery each
函数都在对象数组上操作,返回值是该对象数组。 您不希望您的value
(应该为字符串)接受该返回值。 这是更新的工作小提琴
input.change(function () {
// Input click function...
var value = '';
$(this).parent().find('input').each(function (index, ele) {
value += ele.value;
});
send_request('Date_Off', value);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.