简体   繁体   English

使用javascript设置字段(SharePoint 2013编辑项目表单)

[英]Using javascript to set a field (SharePoint 2013 edit item form)

I am trying to use client side JScript to set a field, however I am not able to do so. 我正在尝试使用客户端JScript设置字段,但是我不能这样做。 Can someone look at the coding? 有人可以看一下编码吗?

<script type="text/javascript">
function setRole() {
    if (ctx.CurrentItem["loggedonuser"].value = "i:0#.w|mickey.mouse") {
        // Setting the IMORole to Yes
        ctx.CurrentItem["IMORole"].value = "Yes";
    }
}

Thanks! 谢谢!

Your if condition is wrong. if条件是错误的。 It should be: 它应该是:

if (ctx.CurrentItem["loggedonuser"].value === "i:0#.w|mickey.mouse") {
        // Setting the IMORole to Yes
        ctx.CurrentItem["IMORole"].value = "Yes";
}

Notice the comparison operator === . 注意比较运算符 ===
= is for assignment and not comparison. =是指分配而不是比较。

Update: 更新:

For finding out whether the value exists in an array or not, you could do one of the following: 为了确定该值是否存在于数组中,可以执行以下操作之一:

  1. Using jQuery - jQuery.inArray() 使用jQuery- jQuery.inArray()
    var exists = $.inArray(value, array);

Example: 例:

var userValue = ctx.CurrentItem["loggedonuser"].value;
var possibleValues = ["str1", "str2"];

var exists = $.inArray(userValue, possibleValues);
if (exists) {
    ctx.CurrentItem["loggedonuser"].value = "Yes";
}
  1. Javascript - Array.prototype.indexOf() Javascript- Array.prototype.indexOf()
    var exists = arrValues.indexOf('Sam') > -1;

Example: 例:

var userValue = ctx.CurrentItem["loggedonuser"].value;
var possibleValues = ["str1", "str2"];

var exists = possibleValues.indexOf(userValue) > -1;
if (exists) {
    ctx.CurrentItem["loggedonuser"].value = "Yes";
}
  1. Javascript (ES2016) - Array.prototype.includes() Javascript(ES2016)-Array.prototype.includes ()
    ["Sam", "Great", "Sample", "High"].includes("Sam");

Example: 例:

var userValue = ctx.CurrentItem["loggedonuser"].value;
var possibleValues = ["str1", "str2"];

var exists = possibleValues.includes(userValue);
if (exists) {
    ctx.CurrentItem["loggedonuser"].value = "Yes";
}

The example below for your reference: 以下示例供您参考:

1.Create a custom list, add a Person or Group field "loggedonuser", add a Yes/No field "IMORole"(default value is No). 1.创建一个自定义列表,添加一个“个人”或“组”字段“ loggedonuser”,添加一个“是/否”字段“ IMORole”(默认值为“否”)。

2.Add the code below into a script editor web part in the editform.aspx. 2.将以下代码添加到editform.aspx中的脚本编辑器Web部件中。

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var loggedonuser=$("input[name^='loggedonuser'][name$='ClientPeoplePicker_HiddenInput']").val();
    if(loggedonuser.indexOf("i:0#.w|mickey.mouse")!=-1){
        $("input[title='IMORole']").prop('checked', true);
    }
});
</script>

To get current login name, we can also use REST API to achieve it. 要获取当前的登录名,我们还可以使用REST API来实现。

<script src="//code.jquery.com/jquery-3.2.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function(){
    var userid = _spPageContextInfo.userId;
    var loginName=getUserLoginName(userid);
    if(loginName=="i:0#.w|mickey.mouse"){
        $("input[title='IMORole']").prop('checked', true);
    }
});
function getUserLoginName(userId){
    var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/web/getuserbyid(" + userId + ")";
    // execute AJAX request
    $.ajax({
        url: requestUri,
        type: "GET",
        async: false,
        headers: { "ACCEPT": "application/json;odata=verbose" },
        success: function (data) {
            loginName=data.d.LoginName;
        },
        error: function () {            
        }
    });
    return loginName;
}
</script>

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

相关问题 Javascript和SharePoint 2013:将背景色设置为列表项 - Javascript and SharePoint 2013: Set background color to list item 在SharePoint 2013中使用JavaScript - Using javascript in sharepoint 2013 SharePoint 2013:以某种形式执行JavaScript - SharePoint 2013: Execute JavaScript in a form 在SharePoint编辑表单中将条件字段设置为只读的问题 - Issue with conditionally set field as read only in SharePoint edit form 使用JavaScript SharePoint 2013在PeopleOrGroup字段中插入用户 - Insert user in PeopleOrGroup Field using javascript SharePoint 2013 如何使用JavaScript或jQuery更改SharePoint 2013编辑表单中“保存”和“取消”按钮的背景颜色? - How can I use JavaScript or jQuery to change the background color of the `Save` and `Cancel` buttons in a SharePoint 2013 edit form? SharePoint 2013在添加项目上运行Javascript - SharePoint 2013 run Javascript on add item 在sharepoint Designer中,创建自定义表单并将其设置为默认表单,但在项目编辑和“项目视图”表单上未打开 - In sharepoint designer create custom form and set as default but on item edit and Item View forms are not open Sharepoint 2013.使用JavaScript的多值查找字段 - Sharepoint 2013. Multivalue lookup field with JavaScript 从SharePoint 2013列表中删除编辑或新项目按钮 - Removing edit or new item button from a SharePoint 2013 list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM