javascript CRM 2011“$ 未定义”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5168471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CRM 2011 "$ is undefined"
提问by MAXA
I have created following code, and I have included this as web resource on the CRM 2011 form to be called on field onchange event of lookup field. Everything is working fine before the $.ajax({...line and then I have an error “$is undefined”.
I am not very familiar with scripting so please help.
我创建了以下代码,并将其作为 Web 资源包含在 CRM 2011 表单中,以便在查找字段的字段 onchange 事件上调用。在该$.ajax({...行之前一切正常,然后我有一个错误“$未定义”。我对脚本不是很熟悉,所以请帮忙。
function GetAddress() {
var accountId;
var dataArray;
var accountRequestUrl;
if (crmForm.all.regardingobjectid.DataValue != null) {
dataArray = crmForm.all.regardingobjectid.DataValue;
accountId = dataArray[0].id;
if (typeof GetGlobalContext == "function") {
var context = GetGlobalContext();
accountRequestUrl = context.getServerUrl();
}
else {
if (typeof Xrm.Page.context == "object") {
accountRequestUrl = Xrm.Page.context.getServerUrl();
}
}
accountRequestUrl = Xrm.Page.context.getServerUrl();
accountRequestUrl += "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'" +
accountId + "')";
crmForm.all.maxlife_addressname.DataValue = accountRequestUrl;
GetAccountRecord(accountRequestUrl);
}
else {
alert("null");
}
}
function GetAccountRecord(accountRequestUrl) {
$.ajax({
type: "GET",
url: accountRequestUrl,
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (request, textStatus, errorThrown) {
alert("Error occurred: " + request.responseXML + "from url " + requestUrl);
return;
},
success: function (data) {
var results = data.d["results"];
var AccountValue = new Array();
for (resultKey in results) {
AccountValue.push(results[resultKey]);
}
FillValues(AccountValue);
}
});
}
回答by Polshgiant
$ is shorthand for jQuery. jQuery is not natively included in CRM2011, so you'll have to add a web reference yourself. Simply create a JavaScript web resource for jQuery, paste in the jQuery code, and then add the web resource to your form. Also, in order to get the web resource to load on your form, you need to specify a function for CRM to call from it. Since in this case jQuery is a library and you won't be calling any of its functions onload, simply use isNaN (a native JavaScript function) as the function to call.
$ 是 jQuery 的简写。jQuery 本身并不包含在 CRM2011 中,因此您必须自己添加 Web 引用。只需为 jQuery 创建一个 JavaScript Web 资源,粘贴 jQuery 代码,然后将 Web 资源添加到您的表单中。此外,为了让 Web 资源加载到您的表单上,您需要为 CRM 指定一个函数来调用它。由于在这种情况下 jQuery 是一个库并且您不会在加载时调用它的任何函数,只需使用 isNaN(原生 JavaScript 函数)作为要调用的函数。
回答by Anish
The entity form on which you are working. Go to Form customization->Form properties. You can see the Files(.js) already included for that form.
您正在处理的实体表单。转到表单自定义-> 表单属性。您可以看到已经包含在该表单中的 Files(.js)。
Click on 'Add'(left top)..and add the JQuery file(like JQuery1.4.4 or higher version) if JQuery file is added in your CRM Webresources, if not then you need to add this file in CRM webresources first.
如果您的 CRM Webresources 中添加了 JQuery 文件,请单击“添加”(左上角)并添加 JQuery 文件(如 JQuery1.4.4 或更高版本),如果没有,则需要先在 CRM Webresources 中添加此文件。
回答by Robert M.
Sounds like you need to include jquery on your form.
听起来您需要在表单中包含 jquery。
Basically you just add jquery the same way you would any other javascript file.
基本上,您只需像添加任何其他 javascript 文件一样添加 jquery。
- Download a copy of jquery (unzip if zipped, you need the .js file)
- Navigate to Web Resources in your Solution
- Click the New button in the toolbar
- Fill in the form
- Click the Browse button for the Upload File box
- Select the .js file you downloaded
- Click the Save button in the ribbon bar
- Click the Publish button in the Ribbon bar
- 下载 jquery 的副本(如果已压缩,则解压缩,您需要 .js 文件)
- 导航到您的解决方案中的 Web 资源
- 单击工具栏中的新建按钮
- 填写表格
- 单击“上传文件”框的“浏览”按钮
- 选择您下载的 .js 文件
- 单击功能区栏中的“保存”按钮
- 单击功能区栏中的“发布”按钮
Add your newly created Web Resource to your form (Under Form Properties).
Be sure this is the first library listed on your form.
You don't need anything in Event Handlers for jquery, just call it from any of your custom libraries as per usual.
将您新创建的 Web 资源添加到您的表单(在表单属性下)。
确保这是表单上列出的第一个库。
您不需要在事件处理程序中为 jquery 提供任何内容,只需像往常一样从您的任何自定义库中调用它即可。
Keep in mind that many of the things you may be tempted to use jquery for may not be supported. Microsoft wants you to use the Xrm.Page object:
Use the Xrm.Page Object Model
http://msdn.microsoft.com/en-us/library/gg328474.aspx
请记住,您可能想使用 jquery 的许多事情可能不受支持。Microsoft 希望您使用 Xrm.Page 对象:
使用 Xrm.Page 对象模型
http://msdn.microsoft.com/en-us/library/gg328474.aspx
According to Microsoft:
Use of jQuery
Do not use jQuery to interact with Microsoft Dynamics CRM 2011 forms.
Use the Xrm.Page object model methods to access form elements.
The only supported use of jQuery in the Microsoft Dynamics CRM 2011 web
application is to use the jQuery.ajax method to retrieve data from
the REST endpoint. For more information, see Using jQuery.
Using jQuery to modify Microsoft Dynamics CRM 2011 application pages or forms is not supported.
You may use jQuery within your own HTML web resource pages.
http://msdn.microsoft.com/en-us/library/gg328261.aspx#BKMK_UsingjQuery>
根据 Microsoft:
使用 jQuery
不要使用 jQuery 与 Microsoft Dynamics CRM 2011 表单交互。
使用 Xrm.Page 对象模型方法访问表单元素。
Microsoft Dynamics CRM 2011 Web
应用程序中唯一支持的 jQuery 使用是使用 jQuery.ajax 方法从
REST 端点检索数据。有关更多信息,请参阅使用 jQuery。
不支持使用 jQuery 修改 Microsoft Dynamics CRM 2011 应用程序页面或表单。
您可以在自己的 HTML 网页资源页面中使用 jQuery。
http://msdn.microsoft.com/en-us/library/gg328261.aspx#BKMK_UsingjQuery>
Use the REST Endpoint with Ajax and JScript Web Resources
http://msdn.microsoft.com/en-us/library/1bb82714-1bd6-4ea4-8faf-93bf29cabaad#BKMK_UsingJQuery
将 REST 端点与 Ajax 和 JScript Web 资源一起使用
http://msdn.microsoft.com/en-us/library/1bb82714-1bd6-4ea4-8faf-93bf29cabaad#BKMK_UsingJQuery
CRM 2011 Useful JavaScript Tidbits
Call the onchange event of a field
http://www.powerobjects.com/blog/2011/01/14/crm-2011-useful-javascript-tidbits/
CRM 2011 有用的 JavaScript 花絮
调用字段的 onchange 事件
http://www.powerobjects.com/blog/2011/01/14/crm-2011-useful-javascript-tidbits/

