我怎样才能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')
可以通过多种方式将其与您现有的react组件集成 - 一种方法可能如下:
/* 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>
);
}
});
我想你的意思是提示():
var userName = prompt('Please Enter your Name')
userName 变量将包含用户答案。