我需要使用什么语法来向一个对象的成员添加一个临时数组,该成员等同于一个字符串的通用列表
What syntax do I need to use to add a temporary array to a member of an object that equates to a generic list of string?
根据这里的答案,我想我可以这样声明并分配给数组:
var _recipients = [];
if ($('#email1').length > 0) {
_recipients.push($('#email1').val());
}
if ($('#email2').length > 0) {
_recipients.push($('#email3').val());
}
if ($('#email3').length > 0) {
_recipients.push($('#email3').val());
}
然后尝试将该数组添加到像这样的对象的成员中:
var saveConfigModel = {
unit: $('#unitsselect').val(),
scheduleProduceUsage: $('#ckbx_produceusage').is(':checked'),
scheduleDeliveryPerformance: $('#ckbx_deliveryperformance').is(':checked'),
scheduleFillRate: $('#ckbx_fillratebycustomer_location').is('checked'),
schedulePriceCompliance: $('#ckbx_pricecompliance').is('checked'),
recipients.push(_recipients),
. . .
但是JSLint抱怨最后一行代码,说:"期望的是‘:’,而看到的是‘.’。"
在更多的上下文中,最后一个是AJAX调用的一部分:
var saveConfigModel = {
unit: $('#unitsselect').val(),
scheduleProduceUsage: $('#ckbx_produceusage').is(':checked'),
scheduleDeliveryPerformance: $('#ckbx_deliveryperformance').is(':checked'),
scheduleFillRate: $('#ckbx_fillratebycustomer_location').is('checked'),
schedulePriceCompliance: $('#ckbx_pricecompliance').is('checked'),
recipients.push(_recipients),
generationDayOfMonth: $('#dayofmonthselect').val(),
generationOrdinal: $('#ordinalselect').val(),
generationDayOfWeek: $('#dayofweekselect').val(),
generationWeekOrMonth: $('#weekormonthselect').val(),
daterangeFromProduceUsage: $('#produsagefrom').val(),
daterangeToProduceUsage: $('#produsageto').val(),
daterangeFromDeliveryPerformance: $('#delperffrom').val(),
daterangeToDeliveryPerformance: $('#delperfto').val(),
daterangeFromFillRate: $('#fillratefrom').val(),
daterangeToFillRate: $('#fillrateto').val(),
daterangeFromPriceCompliance: $('#pricecompliancefrom').val(),
daterangeToPriceCompliance: $('#pricecomplianceto').val()
};
$.ajax({
type: "POST",
url: '@Url.Action("PostUnitConfig", "SaveConfig")',
async: true,
contentType: 'application/json',
dataType: "json",
data: JSON.stringify({ model: saveConfigModel })
});
当我只是给"收件人"添加一个值时,它运行得很好:
recipients: $('#email1').val(),
在服务器上的模型中,收件人是一个字符串的通用列表:
public class UnitConfigVals
{
. . .
public List<string> recipients { get; set; }
. . .
我需要修复什么才能使此数组分配工作?
更改
recipients.push(_recipients),
至
recipients: _recipients
您当前正在调用函数而不是进行赋值。
旁注:你可以用替换你的第一个代码块
var _recipients = $('[id^="email"]').map(function () {
return $(this).val();
}).get().filter(function (y) {
return y != '';
});
相关文章:
- 为什么可以't我使用成员方法初始化一个实例
- 包含一个集合的1到3个成员但其中一个成员不能单独存在的正则表达式
- 我需要使用什么语法来向一个对象的成员添加一个临时数组,该成员等同于一个字符串的通用列表
- 为什么在访问一个新对象的成员之前将其括在括号中
- 影响类的所有成员而不是一个元素事件的插件正在发生
- 角度范围变量中的数组只有一个成员
- 乘以数组成员得到一个新数组
- 我应该如何对具有相同原型的另一个对象的“私有”成员进行操作
- 使用经典继承,在 JavaScript 中使用“类级”/成员变量是一个好主意还是坏主意
- javascript将一个对象添加为另一个对象的成员
- 当其中一个类成员悬停时,突出显示所有类
- javascript中的闭包与c++中的类实例(它有一个私有成员和一个公共方法)相当吗
- 在coffeescript类中是否可以有一个同名的静态函数和成员函数
- 从根JSAPI对象(由prepscript创建)调用另一个JSAPI对象的类成员
- 根据成员值从JSON结果中选择一个成员
- 在javascript中只允许一个类成员的实例
- 创建一个不使用实例id的私有成员
- 使一个外部脚本对qooxdoo Object的所有成员可用
- 为什么必须扩展一个对象才能删除它的成员?
- 如何让javascript对象的一个成员引用同一对象的另一个成员