重新打开表单时,下拉列表变为空白

Dropdown list goes blank, when form is reopened

我在 java-script 中有一个针对国家/地区的下拉列表,states.What 它确实是您从列表中选择一个国家并且状态字段中填充了一个下拉列表状态。完成此操作后,我会保存包含详细信息的表格。但是当我重新打开表格时,国家/地区下拉列表变为空白。

在 CRM 表单的 OnLoad 事件中调用 JS。我已经在 crm-online 2015 中实现了这个。 这是下面的 Java 脚本代码。

有人能指出我正确的方向吗? 非常感谢,

function LoadCountryField(countryFieldName, stateFieldName) {
    var $countryField = $('#' + countryFieldName);
    if ($countryField.length < 1) return;
    $countryField.hide();
    var selectedCountry = $countryField.val();
    var countryRequirementLevel = Xrm.Page.getAttribute(countryFieldName).getRequiredLevel();
    countryRequirementLevel = countryRequirementLevel == "required" ? 2 : countryRequirementLevel == "recommended" ? 1 : 0;
    var $countryDropdown = generateSelectBox('ddl_' + countryFieldName, countryRequirementLevel, Countries, selectedCountry);
    $('#' + countryFieldName + '_d').append($countryDropdown);
    $countryDropdown.change({ 'countryFieldName': countryFieldName, 'stateFieldName': stateFieldName }, handleCountryChanged);
    document.getElementById('ddl_' + countryFieldName).tabIndex = document.getElementById(countryFieldName).tabIndex;
    LoadStateField(stateFieldName, selectedCountry);
}
// Configures the stateOrProvince field to be a dropdown dependent on the value of the country dropdown. Values are pulled from the Countries object.
function LoadStateField(stateFieldName, selectedCountry) {
    var stateAttr = Xrm.Page.getAttribute(stateFieldName);
    var selectedState = stateAttr == null ? "" : stateAttr.getValue();
    var states = getStatesForCountry(selectedCountry);
    var $stateField = $('#' + stateFieldName);
    if (states == null || !$.isArray(states) || states.length < 1) {
        $('#ddl_' + stateFieldName).remove();
        $stateField.show();
        return;
    }
    $stateField.hide();
    var stateRequirementLevel = Xrm.Page.getAttribute(stateFieldName).getRequiredLevel();
    stateRequirementLevel = stateRequirementLevel == "required" ? 2 : stateRequirementLevel == "recommended" ? 1 : 0;
    var $stateDropdown = generateSelectBox('ddl_' + stateFieldName, stateRequirementLevel, states, selectedState);
    var $existingDropdown = $('#ddl_' + stateFieldName);
    if ($existingDropdown.length < 1)
        $('#' + stateFieldName + '_d').append($stateDropdown);
    else
        $existingDropdown.replaceWith($stateDropdown);
    $stateDropdown.change({ 'stateFieldName': stateFieldName }, handleStateChanged);
    $stateDropdown.change();
    document.getElementById('ddl_' + stateFieldName).tabIndex = document.getElementById(stateFieldName).tabIndex;
}
// Finds the states that go with selectedCountry, using the Countries object.
function getStatesForCountry(selectedCountry) {
    for (i in Countries) {
        var country = Countries[i];
        if (selectedCountry == country.name)
            return country.states;
    }
    return [];
}
// Sets the value of the country field to the newly selected value and reconfigures the dependent state dropdown.
function handleCountryChanged(eventData) {
    var stateFieldName = eventData.data.stateFieldName;
    var selectedCountry = setFieldFromDropdown(eventData.data.countryFieldName);
    LoadStateField(stateFieldName, selectedCountry);
}
// Sets the value of the stateOrProvince field to the newly selected value
function handleStateChanged(eventData) {
    setFieldFromDropdown(eventData.data.stateFieldName);
}
// Sets a field's value based on a related dropdown's value
function setFieldFromDropdown(fieldName) {
    var $dropdown = $('#ddl_' + fieldName);
    if ($dropdown.length != 1) return null;
    var selectedValue = $dropdown.find('option:selected:first').val();
    var attr = Xrm.Page.getAttribute(fieldName);
    if (attr != null) attr.setValue(selectedValue);
    return selectedValue;
}
// Generates a new select box with appropriate attributes for MS CRM 2011.
function generateSelectBox(id, requirementLevel, options, selectedValue) {
    var $ddl = $('<select id="' + id + '" class="ms-crm-SelectBox" req="' + requirementLevel + '" height="4" style="IME-MODE: auto; width: 100%"></select>');
    $ddl.append(jQuery('<option></option').val('').html(''));
    $.each(options, function (i, item) {
        $ddl.append(jQuery('<option></option').val(item.name).html(item.name));
        if (selectedValue == item.name)
            $ddl.find('option:last').attr('selected', 'selected');
    });
    return $ddl;
}

**** 无论如何我都能用了**** 感谢大家的建议和帮助

您的代码不受支持,如果您的选项集包含您可以使用相关选项集 MSDN 示例的所有值

https://msdn.microsoft.com/en-us/library/gg594433.aspx

不完全清楚你在做什么,但看起来你使用的是不受支持的 JavaScript,你不应该真正期望它起作用。

这些调用 getElementById,这一点您似乎在 generateSelectBox 添加自定义控件,以及所有 jQuery.

Supported extensions for Microsoft Dynamics CRM

All interaction with Microsoft Dynamics CRM application pages must only be performed through the methods with the Xrm.Page or Xrm.Utility namespaces documented in the Client-side programming reference. Directly accessing the Document Object Model (DOM) of any Microsoft Dynamics CRM application pages is not supported. The use of jQuery in form scripts and commands is not recommended. More information: Use of jQuery

所以我希望用受支持的内容替换尽可能多的不受支持的内容。

就表单重新加载时的这个空白字段而言,可能是因为该字段未保存到数据库。 CRM 需要知道该字段是 dirty 才能提交更改。如果该字段是只读的,有时这会导致问题,因为 CRM 不希望该字段获得 dirty。我建议启用对实体的审计以验证 CRM 是否实际将字段保存到数据库中。