Use HTML5 (datalist) autocomplete with 'contains' approach, not just 'starts with'

前端 未结 4 739
清歌不尽
清歌不尽 2020-11-29 05:03

(I can\'t find it, but then again I don\'t really know how to search for it.)

I want to use and

4条回答
  •  一个人的身影
    2020-11-29 05:46

    I found this question because I wanted "starts with" behavior, and now all the browsers seem to implement "contains". So I implemented this function, which on Firefox (and probably others), if called from input event handler (and optionally, from focusin event handler) provides "starts with" behavior.

    let wrdlimit = prefix =>
    { let elm = mydatalist.firstElementChild;
      while( elm )
      { if( elm.value.startsWith( prefix ))
        { elm.removeAttribute('disabled');
        } else
        { elm.setAttribute('disabled', true );
        }
        elm = elm.nextElementSibling;
      }
    }
    

提交回复
热议问题