简体   繁体   English

jQuery.val() 实际上没有改变值

[英]jQuery .val() Not Actually Changing Value

UPDATE: I feel like I may have left out a key piece of information... The column type is a Person or Group type.更新:我觉得我可能遗漏了一条关键信息......列类型是个人或组类型。 I tried with a single line of text and it works just fine.我尝试了一行文本,它工作得很好。 Doesn't register as empty.But for other entries/pre-filled inputs need to use the Person or Group column to determine other inputs.不注册为空。但对于其他条目/预填充输入需要使用“人员”或“组”列来确定其他输入。 Working with metadata使用元数据

I have a SP list where for my first column, I am having the form autofill the User using an AJAX.我有一个 SP 列表,在我的第一列中,我使用 AJAX 让表单自动填充用户。

It populates the field with the correct value no problem.它用正确的值填充字段没有问题。 My issue is when you can see the value that has been automatically placed there, and hit submit for the form, it still reads the field as blank and tells me "You cannot leave this blank".我的问题是,当您可以看到自动放置在那里的值并点击表单提交时,它仍然将该字段读取为空白并告诉我“您不能将其留空”。

You see the value there, but it is kind of pushed in the left corner of the text box, hidden beside the input boxes default Placeholder text.您会在那里看到该值,但它有点被推到文本框的左角,隐藏在输入框默认占位符文本旁边。 I am wondering if that also could maybe be causing an issue?我想知道这是否也可能导致问题? Is it possible to hide the placeholder text?是否可以隐藏占位符文本?

If I click the text box, and press any key (Enter/space/etc.) then it notices the value automatically inserted.如果我单击文本框,然后按任意键(Enter/空格/等),它会注意到自动插入的值。

Before this gets marked as a duplicate, I have done my due diligence searching through posts and threads and have found similar posts with this issue, but the solution does not work.在这被标记为重复之前,我已经通过帖子和线程进行了尽职调查,并找到了与此问题类似的帖子,但解决方案不起作用。 Like this post here: val() doesn't trigger change() in jQuery喜欢这里的帖子: val() doesn't trigger change() in jQuery

Here is my call and appending of the information:这是我的电话和附加信息:

$(function(){
    $.ajax({
        url: webUri + "/_api/sp.userprofiles.peoplemanager/GetMyProperties",
        type: "GET",
        headers: {
            "Accept": "application/json; odata=verbose"
        },
        success: function(dataCurrentUser){
            console.log(dataCurrentUser)
            $("[title='User']").val(dataCurrentUser.d.AccountName).keyup().change();
        }
    })
})

My test code for your reference:我的测试代码供您参考:

<script>
  $(function () {
    $.ajax({
      url: _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/GetMyProperties",
      type: "GET",
      headers: {
        "Accept": "application/json; odata=verbose"
      },
      success: function (dataCurrentUser) {
        console.log(dataCurrentUser)
        var ppTitle = "people";
        //$("[title='people']").val(dataCurrentUser.d.DisplayName).keyup().change();
        var _PeoplePicker = $("div[title='" + ppTitle + "']");
        var peoplePickerEditor = _PeoplePicker.find("[title='" + ppTitle + "']");
        var _PeoplePickerTopId = _PeoplePicker.attr('id');
        peoplePickerEditor.val(dataCurrentUser.d.DisplayName);
        var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
        ppobject.AddUnresolvedUserFromEditor(true);
      }

    })
  })
</script>

Blog reference: People Picker Field Actions In SharePoint Using JavaScript (JSOM)博客参考: SharePoint 中的人员选取器字段操作使用 JavaScript (JSOM)

Updated:更新:

<script>
    $(function () {
        SP.SOD.executeFunc('clientpeoplepicker.js', 'SPClientPeoplePicker', function () {
            $.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.peoplemanager/GetMyProperties",
                type: "GET",
                headers: {
                    "Accept": "application/json; odata=verbose"
                },
                success: function (dataCurrentUser) {
                    console.log(dataCurrentUser)
                    var ppTitle = "people";
                    //$("[title='people']").val(dataCurrentUser.d.DisplayName).keyup().change();
                    var _PeoplePicker = $("div[title='" + ppTitle + "']");
                    var peoplePickerEditor = _PeoplePicker.find("[title='" + ppTitle + "']");
                    var _PeoplePickerTopId = _PeoplePicker.attr('id');
                    peoplePickerEditor.val(dataCurrentUser.d.DisplayName);
                    var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
                    ppobject.AddUnresolvedUserFromEditor(true);
                }

            })
        })
    });

</script>

Test result:测试结果: 在此处输入图像描述

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

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