SharePoint Online Rest API创建新项目多选选择字段
问题描述:
我有一个SharePoint列表,其中包含一个多选选择字段(复选框).我正在使用HTML/JQuery/AJAX.我试图创建一个新的列表项,但我坚持添加多选选择字段的值.这是我的代码:
I have a SharePoint list with a multi-select choice field (checkboxes). I am using HTML/JQuery/AJAX. I am trying to create a new list item, but I'm stuck on adding the values of the multi-select choice field. Here is my code:
var eff = [];
if ($('input[name="chkA"]').is(':checked')) {
eff.push('"A"');
}
if ($('input[name="chkB"]').is(':checked')) {
eff.push('"B"');
}
if ($('input[name="chkC"]').is(':checked')) {
eff.push('"C"');
}
if ($('input[name="chkD"]').is(':checked')) {
eff.push('"D"');
}
var Effected = '{"__metadata":{"type":"Collection(Edm.String)"},"results":['+ eff.join(',') +']}';
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('ListName')/items",
type: "POST",
data: JSON.stringify
({
__metadata:
{
type: "SP.Data.InternalNameListItem"
},
Title: title,
Effected: Effected
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
"X-HTTP-Method": "POST"
},
success: function(data, status, xhr)
{
alert("Success!");
},
error: function(xhr, status, error)
{
alert("Failed!");
}
});
我在做什么错了?
答
嗨Mscandiga,
请参阅下面的演示(我在某些地方修改了您的代码):
var eff = [];
if (
('input [name ="chkA"]').is(':checked')){ eff.push("A"); //在这里更改 } if(
('input[name="chkA"]').is(':checked')) { eff.push("A"); //change here } if (
('input [name ="chkB"]').is(':checked')){ eff.push("B");//在此处更改 } 如果(
('input[name="chkB"]').is(':checked')) { eff.push("B");//change here } if (