我该如何使用alert()
来允许用户输入他们的名字,并将其保存到状态中?
这是我迄今为止尝试过的:
render: function() {
return (
<div>
<input type="text" onChange={ this.handleChange } />
<Button>Save</Button>
</div>
);
}
}
我该如何使用alert()
来允许用户输入他们的名字,并将其保存到状态中?
这是我迄今为止尝试过的:
render: function() {
return (
<div>
<input type="text" onChange={ this.handleChange } />
<Button>Save</Button>
</div>
);
}
}
一个选项是使用 prompt()
函数,它通过模态对话框显示用户输入,并可获取。 prompt()
方法还允许您提供自定义问候语,可以像这样作为第一个参数传递:
const enteredName = prompt('Please enter your name')
/* Definition of handleClick in component */
handleClick = (event) => {
/* call prompt() with custom message to get user input from alert-like dialog */
const enteredName = prompt('Please enter your name')
/* update state of this component with data provided by user. store data
in 'enteredName' state field. calling setState triggers a render of
this component meaning the enteredName value will be visible via the
updated render() function below */
this.setState({ enteredName : enteredName })
}
render: function() {
return (
<div>
{/* For demonstration purposes, this is how you can render data
previously entered by the user */ }
<p>Previously entered user name: { this.state.enteredName }</p>
<input type="text" onChange={ this.handleChange } />
<input
type="button"
value="Alert the text input"
onClick={this.handleClick}
/>
</div>
);
}
});
我想你是指prompt()函数:
var userName = prompt('Please Enter your Name')
变量 userName 将包含用户输入的答案。