Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何限制react native中textfield的退格事件_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何限制react native中textfield的退格事件

Javascript 如何限制react native中textfield的退格事件,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我想限制用户删除phonenumber字段中的第一个字符。我已经实现了如下所示的onKeyPress事件处理程序,但它仍然没有限制删除文本 onKeyPress={({ nativeEvent }) => { if (nativeEvent.key === "Backspace" && this.props.input.value.charAt(0) === '+') { return false } }} 召唤 在哪里 o

我想限制用户删除phonenumber字段中的第一个字符。我已经实现了如下所示的
onKeyPress
事件处理程序,但它仍然没有限制删除文本

onKeyPress={({ nativeEvent }) => {
      if (nativeEvent.key === "Backspace" && this.props.input.value.charAt(0) === '+') {
        return false
      }
    }}
召唤

在哪里

onKeyPress(event) {}
在函数中


不要使用nativeEvent。

使用onKeyDown而不是onKeyPress,如果条件已通过,请执行e.preventDefault()

函数你的函数(e){

如果(e.code==='Backspace'&&e.target.value.charAt(0)=='+'&&e.target.selectionStart如果您不希望他们更改它,您是否考虑过不将其包含在字段中? e、 g.(香草HTML)

4

其中“4”是纯文本,无法更改。

不起作用。
nativeEvent.preventDefault()
不是函数错误。如果用户用另一个字符更改您的第一个字符,它将不起作用。请尝试从输入中删除此字符,稍后再处理它。我不知道,但
preventDefault()
在react native中生成错误。是否可以尝试使用setNativeProps
onKeyPress(event) {}
4<input type="tel"/>