Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/388.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 js中单击确认警报后在页面中显示消息_Javascript_Html_Reactjs - Fatal编程技术网

Javascript 如何在react js中单击确认警报后在页面中显示消息

Javascript 如何在react js中单击确认警报后在页面中显示消息,javascript,html,reactjs,Javascript,Html,Reactjs,在我下面使用react js的代码中,当我单击确认警报ok时,我想使其在网页中显示消息Successful。在我下面的代码中,当我单击按钮时,其确认警报显示,当我单击ok时,则消息不显示在网页中 如果有任何帮助,我们怎么做。非常感谢 任何人都可以帮我解决这个问题 请在我的代码中检查我试图实现的目标 您可以使用const res=window。确认(“删除项目?”)要进行检查,请从警报中单击确定或取消。之后,当用户单击OK时,您可以根据需要显示消息success 您可以使用const res=wi

在我下面使用react js的代码中,当我单击确认警报ok时,我想使其在网页中显示消息Successful。在我下面的代码中,当我单击按钮时,其确认警报显示,当我单击ok时,则消息不显示在网页中

如果有任何帮助,我们怎么做。非常感谢

任何人都可以帮我解决这个问题

请在我的代码中检查我试图实现的目标


您可以使用
const res=window。确认(“删除项目?”)要进行检查,请从
警报
中单击
确定
取消
。之后,当用户单击OK时,您可以根据需要显示消息success


您可以使用
const res=window。确认(“删除项目?”)要进行检查,请从
警报
中单击
确定
取消
。之后,当用户单击OK时,您可以根据需要显示消息success


我真的不明白你想做什么

您已使用代码显示问题提示。 如果您想删除该唯一记录,只需向类组件添加一个ref,如下所示:

constructor(props) {
    super(props);
    this.myRef = React.createRef(null);
}
然后添加处理删除逻辑的功能,例如:发出POST请求以删除特定项目

handleRemoveItem = (e) => {

    const res = window.confirm("Delete the item?");
    /**
     * Probably a POST request to delete 
     * the record with the key 'e'.
     * Or handle múltiples refs.
     */ 
    if (res)
      this.myRef.current.remove();
}
将ref添加到按钮,您已经完成了其余操作:

<button
  className="btn btn-danger"
  ref={this.myRef}
  onClick={this.handleRemoveItem}
>
最后,处理程序的逻辑是:

handleRemoveItem = (e) => {

    const res = window.confirm("Delete the item?");
    /**
     * Probably a POST request to delete 
     * the record with the key 'e'.
     * Or handle múltiples refs.
     */ 
    if (res) {
      this.myRef.current.remove();
      this.setState({ success: true });
      setTimeout(() => this.setState({ success: false }), 3000);
    } else {
      this.setState({ error: true });
      setTimeout(() => this.setState({ error: false }), 3000);
    }
}

我真的不明白你想做什么

您已使用代码显示问题提示。 如果您想删除该唯一记录,只需向类组件添加一个ref,如下所示:

constructor(props) {
    super(props);
    this.myRef = React.createRef(null);
}
然后添加处理删除逻辑的功能,例如:发出POST请求以删除特定项目

handleRemoveItem = (e) => {

    const res = window.confirm("Delete the item?");
    /**
     * Probably a POST request to delete 
     * the record with the key 'e'.
     * Or handle múltiples refs.
     */ 
    if (res)
      this.myRef.current.remove();
}
将ref添加到按钮,您已经完成了其余操作:

<button
  className="btn btn-danger"
  ref={this.myRef}
  onClick={this.handleRemoveItem}
>
最后,处理程序的逻辑是:

handleRemoveItem = (e) => {

    const res = window.confirm("Delete the item?");
    /**
     * Probably a POST request to delete 
     * the record with the key 'e'.
     * Or handle múltiples refs.
     */ 
    if (res) {
      this.myRef.current.remove();
      this.setState({ success: true });
      setTimeout(() => this.setState({ success: false }), 3000);
    } else {
      this.setState({ error: true });
      setTimeout(() => this.setState({ error: false }), 3000);
    }
}

您共享的代码沙盒是“记录”消息,而不是显示在屏幕上。我已经根据用户选择的选项更新了代码以更新状态

  handleAgree = () => {
    this.setState({ agreedState: "I agree!" });
    this.handleClose();
  };

  handleDisagree = () => {
    this.setState({ agreedState: "I do not agree!" });
    this.handleClose();
  };
最后,我们可以显示消息:

{/* Showing message according to the selected option */}
<h1>{this.state.agreedState}</h1>
{/*根据所选选项显示消息*/}
{this.state.agreedState}

以下是更新后的消息。

您共享的代码沙盒是“记录”消息,而不是显示在屏幕上。我已经根据用户选择的选项更新了代码以更新状态

  handleAgree = () => {
    this.setState({ agreedState: "I agree!" });
    this.handleClose();
  };

  handleDisagree = () => {
    this.setState({ agreedState: "I do not agree!" });
    this.handleClose();
  };
最后,我们可以显示消息:

{/* Showing message according to the selected option */}
<h1>{this.state.agreedState}</h1>
{/*根据所选选项显示消息*/}
{this.state.agreedState}

这是更新的。

有人能帮我解决这个问题吗?在那里更新了您的代码,这是显示消息的一种方式,但实际上取决于您的用例@另一位编码员感谢您在此处回复我想在选择“同意”或“不同意”后显示成功和失败消息我们如何在该代码中做到这一点您可以帮助我吗?请帮助我解决这一问题?更新您的代码,这是显示消息的一种方式,但实际上取决于您的用例@另一位编码员感谢您在此回复我想在选择“同意”或“不同意”后显示“成功”和“不成功”消息在这段代码中我们如何才能做到这一点请帮助我在这段代码中感谢我们如何处理在这段代码中我们如何确认代码不工作其显示错误我想在单击窗口警报后显示“成功”或“失败”消息添加了您需要的内容。您的代码不工作,显示错误我想在单击窗口警报后显示成功或失败消息我添加了您需要的内容。