简体   繁体   English

SharePoint 将 Javascript 事件添加到 editForm.aspx 页面

[英]SharePoint add Javascript event to a editForm.aspx page

I have a Sharepoint list where I want to customize the editForm.aspx page with Javascript.我有一个 Sharepoint 列表,我想在其中使用 Javascript 自定义 editForm.aspx 页面。

The problem is that I can't manage to add any events to the controls in that page.问题是我无法向该页面中的控件添加任何事件。 I am able to get a reference to my control with this code:我可以使用以下代码获得对我的控件的引用:

var textarea1;
var objForm = document.forms[0];
function FindField() {
    var title;
    title= $().SPServices.SPGetDisplayFromStatic({ listName: "MyList", columnStaticName: "mycolumn" });
    textarea1 = ChooseFieldByTitle("TextField", title);
}

function ChooseFieldByTitle(TypeField, title) {
    var elem;
    for (idx = 0; idx < objForm.elements.length; idx++) {
        elem = objForm.elements[idx];

        if (elem.id.indexOf(TypeField) != -1 &&
        elem.title == title) {
            return elem;
        }
    }

    return null;
};

This runs fine but I can't attach any event on this control.这运行良好,但我无法在此控件上附加任何事件。 I have tried the following code (Field is a textArea):我尝试了以下代码(字段是文本区域):

 Field.attachEvent("onchange", myFunction);
 Field.addEventListener("onchange", myFunction, false);
Field.onchange = myFunction() {};

When I change the content of the textArea nothings happen.当我更改 textArea 的内容时,什么也没有发生。 When I debug with IE developers tools "myFunction" is never called.当我使用 IE 开发人员工具进行调试时,从未调用过“myFunction”。 What am I doing wrong?我究竟做错了什么? I have look at SPServices JQuery library but nothing seems to be related to Javascript events.我查看了 SPServices JQuery 库,但似乎与 Javascript 事件无关。

Does anyone have a clue?有人有线索吗?

Try to use Jquery, this is a Javascript library that is used a lot.尝试使用Jquery,这是一个使用很多的Javascript库。 It makes for instance selecting objects easier.例如,它使选择对象更容易。 If you want to combine it with spservices (or another library), add the following lines in your aspx page under the PlaceHolderMain:如果要将其与 spservices(或其他库)结合使用,请在您的 aspx 页面中的 PlaceHolderMain 下添加以下行:

<asp:Content ContentPlaceHolderId="PlaceHolderMain" runat="server">
<script language="javascript" type="text/javascript" src="/SiteAssets/js/jquery-1.9.0.js"></script>
<script language="javascript" type="text/javascript" src="/SiteAssets/js/jquery.SPServices-0.7.2.min.js"></script>
<script language="javascript" type="text/javascript" src="/SiteAssets/js/myjavascriptfile.js"></script>

You can then select objects on your page via jquery and immediately tie to the events eg:然后,您可以通过 jquery 选择页面上的对象并立即关联到事件,例如:

$(":input[title$='atitleofsomefield']").keyup(function() {                  
                  // do stuff               
            })

Here is a list of Jquery events: http://api.jquery.com/category/events/以下是 Jquery 事件列表: http : //api.jquery.com/category/events/

The "onchange" event is fired by a dropdown box when you select a new value.当您选择一个新值时,下拉框会触发“onchange”事件。 It won't fire for a Textarea.它不会为 Textarea 触发。 In your case you may want to check the KeyUp event for example, or the Blur event.例如,在您的情况下,您可能想要检查 KeyUp 事件或 Blur 事件。

Also, you may want to use jQuery or another library to handle the events.此外,您可能希望使用 jQuery 或其他库来处理事件。

The only way that I have found to accomplish this is via a timer.我发现实现此目的的唯一方法是通过计时器。 Look at something like this:看看这样的事情:

$(document).ready(function() { 
    var initialValue = undefined;
    Field.on('change', function(){
        //If the value has changed, run the function
        if(initialValue==undefined || Field.val()!=initialValue){
            initialValue = Field.val();
            myFunction();
        });
    //Reinitialize the timer 
    checkForChange();
});
//Timer function to throw the 'changed' event
function checkForChange() {
    setTimeout(triggerChange, 1500);
}
//Send the changed event for Field
function triggerChange(){
    Field.trigger('change');
}

And then within your function you do your regular logic.然后在您的函数中执行常规逻辑。

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

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