[英]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: 为了确定该值是否存在于数组中,可以执行以下操作之一:
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";
}
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";
}
["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.