【问题标题】:Date field does not appear properly In react日期字段未正确显示在反应中
【发布时间】:2015-09-23 11:52:09
【问题描述】:

我有一个输入字段,其类型为日期,占位符为“输入您的日期”。但是发生的事情是占位符没有出现在移动应用程序中。 (我们正在使用 html、css、bootstrap 和 react.js 构建移动应用程序并通过cordova移植“)。所以我正在关注这个链接,以显示标签,点击时应该出现date pop。但这似乎是不起作用,当检查 chrome 中的元素时,onfocus 和 onblur 似乎正在消失。有没有办法让它在反应中工作。

<ElementsBasket name='nextActionDate' 
                      data={this.props.newLead.get('actions').get('nextActionDate')}>
    <div className="form-group">

       <input type="text" className="form-control" onfocus="(this.type='date')" onblur="(this.type='date')"
                       placeholder="Type your date here.." 
                       value={this.props.newLead.get('actions').get('nextActionDate')}
                       onChange={onFieldChanged.bind(this, 'actions.nextActionDate')}
                       />       
     </div>
</ElementsBasket>

ps:我正在关注此链接 Not showing placeholder for input type="date" field

【问题讨论】:

    标签: javascript cordova reactjs


    【解决方案1】:

    在 React 中,你的事件应该被称为 onFocusonBlur(注意大写)

    https://facebook.github.io/react/docs/events.html

    编辑:

    另一个问题是您将处理程序设置为字符串 - onFocus="(this.type='date')",它们应该是一个函数。所以像设置 onChange 事件一样设置它们 -

     <input type="text" onFocus = {this._onFocus} onBlur={this._onBlur}
    

    现在this 指的是包含您的渲染函数的对象,因此您需要在该对象中实现适当的函数。 例如。

    _onFocus: function(e){
        e.currentTarget.type = "date";
    },
    _onBlur: function(e){
        e.currentTarget.type = "text";
        e.currentTarget.placeholder = "Enter a Date";
    },
    

    【讨论】:

    • 问题仍然存在,我做了大写。它们没有(onFocus 和 onBlur)加载!
    • 工作正常,除了有一个问题。我需要单击日期字段两次。第一次 type="text" 和占位符是“Next Action Date”。当我使用 onFocus type="date" 时。因此,如果我单击 Next Action Date,onFocus 将被触发,并且再次出现一个白色按钮字段,它没有任何作用。整个问题出在这个空按钮上。我不想要这个按钮。所以当我点击这个按钮时,日历就会出现。无论如何,我可以单击占位符 Next Action Date 并显示日历吗?我也试过onClick。但是没有结果,也是分了两步
    • 我不这么认为(看看这里stackoverflow.com/questions/24975667/…)。现在这是一个不同的问题,可能会因浏览器(Android 版本)而异。老实说,我不认为像这样编写代码来切换输入类型是一个好主意,并且可能会导致问题。我只是在回答你关于为什么 onfocus 和 onblur 在 React 中不起作用的问题
    • 是的,即使我也有同样的感觉,但按钮上没有文字似乎不太好。
    【解决方案2】:

    这个比较简单

    <input
       type="text"
       onFocus={
        (e)=> {
          e.currentTarget.type = "date";
          e.currentTarget.focus();
         }
       }
       placeholder="dd/mm/yyyy"
    />
    
    

    【讨论】:

      【解决方案3】:

      基本上 date 类型带有默认占位符,即 mm/dd/yyyy,这就是为什么我们的 placeholder 属性 不显示为 text 类型。

      所以我们可以使用事件处理程序onFocusonBlur 使其工作。 我们可以像这样在 箭头函数 结构中添加这些处理程序 -

      <input
        type="text"
        onFocus={(e) => (e.currentTarget.type = "date")}
        onBlur={(e) => (e.currentTarget.type = "text")}
        placeholder="No date found"
      />
      

      output will be like this

      ReactDOM.render(
          <input
            type="text"
            className="form-control"
            onFocus={(e) => (e.currentTarget.type = "date")}
            onBlur={(e) => (e.currentTarget.type = "text")}
            placeholder="No date found"
          />,
        document.getElementById("react")
      );
      .form-control {
        padding: 10px;
        font-size: 18px;
      }
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
      <div id="react"></div>

      希望对你有帮助,谢谢

      【讨论】:

        【解决方案4】:

        当输入类型是 Date 时,显然你不能带入占位符,解决这个问题的一种方法是动态更改输入类型 on-focus ,但这不是一个好习惯,我的建议是不要去使用默认日期选择器,您可以使用下拉菜单

        你也可以参考这个question。希望这对你有帮助:)

        【讨论】:

        • 请查看我问题的ps中的链接
        • 好的检查,但似乎他也想说你应该改变类型!并且您的 onfocus 和 onblur 正在做同样的事情,如果您使用他的逻辑,那么您应该将类​​型更改为 text onblur
        • 即使这样问题也没有解决。 onfocus 和 onblur 甚至没有加载到反应中
        • 你能不能试着用onKeydown替换onchange
        • 老兄,我的问题是 onfocus 和 onblur 甚至没有加载,在我的情况下,onfocus 会使 type=date,所以这不会被解雇
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 2019-09-24
        • 2019-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多