Does React.js support HTML5 datalist?

删除回忆录丶 提交于 2019-12-10 12:44:55

问题


I'm trying to implement HTML5 datalist element in a easiest possible way.

Something like this:

<input list="browsers">

<datalist id="browsers">
  <option value="Internet Explorer">
  <option value="Firefox">
  <option value="Chrome">
  <option value="Opera">
  <option value="Safari">
</datalist>

This does not work however. What would be my next step without having to install (npm) additional stuff.

Basically, I'm using the plain input react element and want to embed the datalist.

Here is my react code:

    <input className={"custom_input inline "+this.isValidInteger(this.props.price.price_first,0,2000000)} 
    style={{marginRight:'5px'}} 
    value={this.props.price.price_first || ''} type="text"
    onChange={(e)=>this.props.dispatch({type:"price", payload:e.target.value})} 
    placeholder=" Unesite..." 
    maxLength="10"/>

So I would like a dropdown on top of that.


回答1:


I'm currently using the html5 datalist in react without issues.

Here goes the implementation:

<input type="text" list="data" onChange={this._onChange} />

  <datalist id="data">
    {this.state.data.map((item, key) =>
      <option key={key} value={item.displayValue} />
    )}
  </datalist>



回答2:


It takes a little change to make MDN datalist example works in React.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/datalist

<label>
    Choose a browser from this list:
    <input list="browsers" name="myBrowser" />  
</label>   
<datalist id="browsers">
    <option value="Chrome" />
    <option value="Firefox" />
    <option value="Internet Explorer" />
    <option value="Opera" />
    <option value="Safari" />
    <option value="Microsoft Edge" />   
</datalist>



回答3:


Datalists work fine in React, but you must close each option tag (with a backslash at the end).

<option value="something" />


来源:https://stackoverflow.com/questions/45167565/does-react-js-support-html5-datalist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!