下拉字段 - 第一项应为空 [英] Dropdown field - first item should be blank

查看:190
本文介绍了下拉字段 - 第一项应为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在查找列中使用sharepoint构建,并将其设置为必填字段。 SharePoint自动选择下拉框中的第一个项目(对最终用户有误导性)。

Using sharepoint build in lookup column and it set to required field. SharePoint automatically selects the first item in the dropdown box (kinda misleading for the end users).

有没有一种方式可以显示此drop的第一行的空白或Null下拉框?

Is there a way to display blank or Null for the first row of this drop down box?

(我对任何解决方案都很开放,我更喜欢javascript类型的解决方案)

(I am open to any solution. I prefer javascript type solution)

推荐答案

对于选择字段,默认值在列设置中配置。如果填写了默认值输入框,请删除该值以便不使用默认值。

For Choice fields, the default value is configured in the column settings. If the "Default value" input box is populated, delete the value in order to use no default value.

修改

对于Lookup字段,如果需要,该字段似乎会发生显着变化。不需要的字段默认为(无)值。但是,将该字段切换到所需的值将删除(无)值,并自动选择第一个值。

For Lookup fields, the field seems to change dramatically if it is required. Fields that are NOT required have a "(None)" value by default. However, toggling the field to required will remove the "(None)" value and the first value is selected automatically.

有一件事我发现,如果您使用JavaScript将null值添加到下拉列表中,然后尝试按OK,您将收到错误页面:发生意外错误。作为解决方法,我写了一些更多的代码来进行快速验证,该字段在提交表单之前有一个值。如果该字段没有值,则会提示用户并取消提交。 (注意:此代码仅附加到OK按钮,因此您可能在编辑EditForm.aspx时可能会收到错误。只需为查找字段选择一个值即可正常编辑)

One thing I found, is that if you use JavaScript to add the null value to the dropdown and then try to press OK you get an error page: "An unexpected error has occurred." As a workaround, I wrote some more code to do a quick validation that the field has a value before the form is submitted. If the field has no value, then it will prompt the user and cancel the submit. (Note: this code is only attached to the OK buttons so you may get errors while editing EditForm.aspx.. just choose a value for your lookup field and you'll be able to edit like normal)

无论如何,代码...我认为您需要更改的唯一一行是 var fieldTitle ='Large Lookup Field'; 将其更新为您的字段名称。

Anyways, onto the code... I think the only line you'll need to change is var fieldTitle = 'Large Lookup Field'; to update it to the name of your field.

<script type="text/javascript">

function GetDropdownByTitle(title) {
    var dropdowns = document.getElementsByTagName('select');
    for (var i = 0; i < dropdowns.length; i++) {
        if (dropdowns[i].title === title) {
            return dropdowns[i];

        }
    }
    return null;
}

function GetOKButtons() {
    var inputs = document.getElementsByTagName('input');
    var len = inputs.length;
    var okButtons = [];
    for (var i = 0; i < len; i++) {
        if (inputs[i].type && inputs[i].type.toLowerCase() === 'button' && 
             inputs[i].id && inputs[i].id.indexOf('diidIOSaveItem') >= 0) {
             okButtons.push(inputs[i]);
        }
    }
    return okButtons;
}

function AddValueToDropdown(oDropdown, text, value, optionnumber){
    var options = oDropdown.options;
    var option = document.createElement('OPTION');
    option.appendChild(document.createTextNode(text));
    option.setAttribute('value',value);
    if (typeof(optionnumber) == 'number' && options[optionnumber]) {
        oDropdown.insertBefore(option,options[optionnumber]);
    }
    else {
        oDropdown.appendChild(option);
    }
    oDropdown.options.selectedIndex = 0;
}

function WrapClickEvent(element, newFunction) {
    var clickFunc = element.onclick;
    element.onclick = function(event){
        if (newFunction()) {
            clickFunc();
        }
    };
}

function MyCustomExecuteFunction() {
    // find the dropdown
    var fieldTitle = 'Large Lookup Field';
    var dropdown = GetDropdownByTitle(fieldTitle);
    if (null === dropdown) {
        alert('Unable to get dropdown');
        return;
    }

    AddValueToDropdown(dropdown, '', '', 0);

    // add a custom validate function to the page
    var funcValidate = function() {
        if (0 === dropdown.selectedIndex) {
            alert("Please choose a value for " + fieldTitle + ".");
            // require a selection other than the first item (our blank value)
            return false;
        }
        return true;
    };

    var okButtons = GetOKButtons();
    for (var b = 0; b < okButtons.length; b++) {
        WrapClickEvent(okButtons[b], funcValidate);
    }
}

_spBodyOnLoadFunctionNames.push("MyCustomExecuteFunction");

</script>

这篇关于下拉字段 - 第一项应为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆