简体   繁体   English

如果嵌套在表单和div中的按钮重新呈现,则提交自动触发

[英]Submit auto triggered if button nested inside form and divs is re rendered

Developing a React JS web app, I've encountered a weird issue that is happening to me right now and can't find out why. 在开发React JS网络应用程序时,我遇到了一个奇怪的问题,该问题现在正在我身上发生,并且找不到原因。

    getButtonAction() {
    const buttonEditar = <button className="btn_inform" type="button" name="button" onClick={this.toggleModoEdicion} >
                            <img src={icon_editar} alt="Editar" />
                            <p>Editar</p>
                        </button>;

    const buttonConfirmar = <button className="btn_inform" type="submit" >
                                <div className="icon">
                                    <img src={icon_tilde_violeta} alt="Confirmar" />
                                </div>
                                <p>Confirmar</p>
                            </button>;

    return !this.state.modo_edicion ? buttonEditar : buttonConfirmar;
}

If I put the form this way (notice where the button Action is): 如果我以这种方式放置表单(请注意按钮Action的位置):

    let imagenPerfil = isNullOrUndefined(this.state.imgPerfil) ? icon_user_ejemplo : this.state.imgPerfil;

    const headerContacto = this.state.tipo_usuario == 0 ? <h2> <span>Datos de contacto</span> </h2> : null;
    let buttonAction = this.getButtonAction();

    return (  
        <section id="box_r_usuarios">
            <form onSubmit={this.handleSubmit}>
                {buttonAction} <-- BUTTON ACTION
                <div className="top">
                    <div className="box">
                        <div className="avatar">
                            <InputImage img={imagenPerfil} name="imgPerfil" onChange={this.handleInputChange} disabled={!this.state.modo_edicion} />
                        </div>
                        <div className="box_info">
                            <div className="info">
                                <div className="icon">
                                    <img src={icon_membresia} alt="Membresía Premium" />
                                </div>
                                <p>Membresía Premium</p>
                            </div>
                            <div className="info">
                                <div className="icon">
                                    <img src={icon_nivel} alt="Nivel Intermedio" />
                                </div>
                                <p>Nivel Intermedio</p>
                            </div>
                        </div>
                    </div>
                    <div className="box">
                        <div className="right">
                            <button className="btn_inform" type="button">
                                <div className="icon">
                                    <img src={icon_tutor} alt="Tutor Asignado" />
                                </div>
                                <p>Tutor Asignado</p>
                            </button>
                        </div>
                    </div>

                </div>
 etc etc

The form behaves as expected. 该窗体的行为符合预期。

However if put the buttonAction as in here: 但是,如果按如下所示放置buttonAction:

    let imagenPerfil = isNullOrUndefined(this.state.imgPerfil) ? icon_user_ejemplo : this.state.imgPerfil;

    const headerContacto = this.state.tipo_usuario == 0 ? <h2> <span>Datos de contacto</span> </h2> : null;
    let buttonAction = this.getButtonAction();

    return (  
        <section id="box_r_usuarios">
            <form onSubmit={this.handleSubmit}>
                <div className="top">
                    <div className="box">
                        <div className="avatar">
                            <InputImage img={imagenPerfil} name="imgPerfil" onChange={this.handleInputChange} disabled={!this.state.modo_edicion} />
                        </div>
                        <div className="box_info">
                            <div className="info">
                                <div className="icon">
                                    <img src={icon_membresia} alt="Membresía Premium" />
                                </div>
                                <p>Membresía Premium</p>
                            </div>
                            <div className="info">
                                <div className="icon">
                                    <img src={icon_nivel} alt="Nivel Intermedio" />
                                </div>
                                <p>Nivel Intermedio</p>
                            </div>
                        </div>
                    </div>
                    <div className="box">
                        <div className="right">
                            <button className="btn_inform" type="button">
                                <div className="icon">
                                    <img src={icon_tutor} alt="Tutor Asignado" />
                                </div>
                                <p>Tutor Asignado</p>
                            </button>
                {buttonAction}<-- This is where the BUTTON ACTION is now
                        </div>
                    </div>

                </div>
etc etc

When button action is re rendered because of the change of state, the submit of the form is triggered. 由于状态更改而重新呈现按钮操作时,将触发表单的提交。

So if I set the state to modo_edicion : true, then the button gets re rendered, and the form is submited without any action from the user. 因此,如果将状态设置为modo_edicion:true,则按钮将重新呈现,并且无需用户采取任何操作即可提交表单。 Drove me nuts this last hour and 最后一个小时让我发疯

I hope I'm explaining myself clearly. 我希望我能清楚地解释自己。

I am not sure if I understood your problem. 我不确定是否理解您的问题。 but here we go... 但是我们开始...

Have you noticed that just one of the buttons are type "submit"? 您是否注意到只有一个按钮是“提交”类型? Are you forgetting the close tag ? 您是否忘了关闭标签?

if nothing of the above make sence, try to remove the onsubmit event and call the method where you need it. 如果上述都不起作用,请尝试删除onsubmit事件,并在需要时调用该方法。

Thanks all of you! 谢谢大家!

Solved it making separate methods for both buttons and hiding them according to the state. 解决了对两个按钮分别设置方法并根据状态隐藏它们的问题。 Weird issue but the workaround works. 问题很奇怪,但是解决方法可行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM