【问题标题】:sharepoint jquery autocomplete clear textbox if no value如果没有值,sharepoint jquery 自动完成清除文本框
【发布时间】:2016-02-09 09:08:31
【问题描述】:

我创建了基于另一个列表中的数据的自动完成功能的文本框。如果用户没有选择任何项目或者他输入了错误的文本,我需要清除文本框。下面是我的代码

$.ajax({
        url: "http://address/_vti_bin/lists.asmx",
        type: "POST",
        dataType: "xml",
        data: soapEnv,
        contentType: "text/xml; charset=\"utf-8\"",
        success: function (xmlResponse) {
            var domElementArray = $("z\\:row", xmlResponse);
            var dataMap = domElementArray.map(function () {
                return {
                    value: $(this).attr('ows_AirportCode'),
                    id: $(this).attr('ows_AirportCode')
                };
            });
            var data = dataMap.get();

            //Find the Sharepoint Portal Search Box (this is a poor selector, but it is not properly named by sharepoint, well it is but INamingContainer getrs in the way)   
            $("input[title='AirportCode Required Field']").autocomplete(
        {
            source: data,

            miniLength: 3,
            response: function (event, ui) {
                // ui.content is the array that's about to be sent to the response callback.
                if (ui.content.length == 0) {
                    $("#empty-message").text("No results found");
                } else {
                    $("#empty-message").empty();
                }
            }
        }
        );
        }
    }); //.ajax  

谢谢

【问题讨论】:

    标签: jquery sharepoint autocomplete


    【解决方案1】:

    您可以在自动完成时订阅更改事件,并将所选值与您的列表值数组进行比较以进行匹配:

    .autocomplete({
       ...
       .change: function( event, ui ) {
          var input = $("input[title='AirportCode Required Field']");
          var selectedValue = input.val();
          var valid = false;
          $.each(data , function (i, item) {
             if (item.value === selectedValue) valid = true;
          });
    
          if (valid == false){
            // clear the input if not found
            input.val('');
          }
       }
    })
    

    【讨论】:

      猜你喜欢
      • 2015-11-28
      • 1970-01-01
      • 2012-07-21
      • 2012-10-28
      • 2023-03-17
      • 2011-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多