简体   繁体   English

jQuery spservices更新listitem

[英]jquery spservices update listitem

i'm trying to update a listitem using jquery spservices. 我正在尝试使用jquery spservices更新listitem。 everything is working but when i try to add a href to a richtextfield it doesnt work. 一切正常,但是当我尝试将href添加到richtextfield时,它不起作用。 it only updates plain text not the href. 它仅更新纯文本,而不更新href。 below is code, it's just a test so those urls are for testing. 下面是代码,它只是一个测试,因此这些URL都用于测试。

function fn_UpdateListItem(){
$().SPServices({
operation: 'UpdateListItems',
listName: 'Bedrijven',
ID: 1,
valuepairs: [["Software", "<a href='http://www.google.nl'>its a test.</a>"]],
completefunc: function(xData, Status) {
alert('test complete');
}
});
}

if i change the valuepairs to 如果我将值对更改为

valuepairs: [[\"Software\", \"test\"]],

it works it puts test in the rich text field. 它可以将测试置于RTF文本字段中。 but with href it doesnt work. 但是使用href无效。 anyone knows how to fix ? 有谁知道如何解决? thanks in advane 感谢先进

I got the same problem for Sharepoint 2010, in this case the var dfNotes = CKEDITOR.instances.notes.getData(); 对于SharePoint 2010,我遇到了同样的问题,在这种情况下, var dfNotes = CKEDITOR.instances.notes.getData(); didn't work for me, i found this: 没有为我工作,我发现了这一点:

https://msdn.microsoft.com/en-us/library/office/ee658527(v=office.14).aspx https://msdn.microsoft.com/en-us/library/office/ee658527(v=office.14).aspx

var value = SP.Utilities.HttpUtility.htmlEncode(html); 
Edit 编辑

Tested on Sharepoint 2016 SharePoint On-Premises , It works too, so i it should work for SharePoint Online aswell !! Sharepoint 2016 SharePoint内部部署上进行了测试,它也可以工作,所以我也应该在SharePoint Online上工作!

This is how it worked for me: 这对我来说是这样的:

function AddListItem(html, list) { 
    var value = SP.Utilities.HttpUtility.htmlEncode(html);        
    $().SPServices({
        operation: "UpdateListItems",
        async: false,
        batchCmd: "New",
        listName: list,
        valuepairs: [["Title", 'Title'], ["Content", value]],
        completefunc: function(xData, Status) {
            console.log(Status);
        }
    });

} 

Here is the actual solution. 这是实际的解决方案。 Prior to submitting to a SharePoint list, the HTML-ized data are stored in XML, which doesn't take kindly to embedded HTML tags, so they need to be escaped. 在提交到SharePoint列表之前,将HTML格式的数据存储为XML,这对嵌入式HTML标记并不友好,因此需要对其进行转义。 Thanks to feedback on the SPServices forum, I was able to determine this was the case in my example above. 感谢SPServices论坛上的反馈 ,我能够确定上述示例中的情况。

I revised my code to look like this: 我修改了代码,使其看起来像这样:

var dfNotes = CKEDITOR.instances.notes.getData();

$().SPServices({
    operation: "UpdateListItems",
    async: false,
    batchCmd: "Update",
    listName: list,
    ID: prog,
    valuepairs: [["Notes", $("#notes").text(dfNotes).html()]],
    completefunc: function (xData, Status) {
        alert($("#notes").html());
    }
});

The first line references a rich text editor field that contains newly modified text. 第一行引用富文本编辑器字段,其中包含新修改的文​​本。 Note the slight difference in the valuepairs line where it now uses .text().html() to escape the text for transmission via XML. 请注意, valuepairs行中的细微差异现在使用.text().html()来转义文本以通过XML传输。

I hope this helps someone! 我希望这可以帮助别人!

You need encode the html code (replace the characters < and > for JavaScript: Escaping Special Characters, &lt; and &gt; ; this is an example of some characters ), this way you going to have a string available to save in the rich content text field (Notes), when The item is updated your data going to have a html code. 您需要对html代码进行编码(将JavaScript的字符<和>替换为JavaScript:转义特殊字符, &lt;&gt; ;; 这是一些字符的示例 ),这样,您将可以使用字符串来保存丰富内容文本字段(注释),当项目更新时,您的数据将具有html代码。

This is the code: 这是代码:

function fn_UpdateListItem(){

    var link = htmlEscape('<a href='http://www.google.nl'>its a test.</a>');

    $().SPServices({
        operation: 'UpdateListItems',
        listName: 'Bedrijven',
        ID: 1,
        valuepairs: [["Software", link]],
        completefunc: function(xData, Status) {
            alert('test complete');
        }
     });
}

//This function makes the magic
function htmlEscape(str) {
    return String(str)
        .replace(/&/g, '&amp;')
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
}

Best regards 最好的祝福

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM