简体   繁体   English

单击单独的 div 会导致单选按钮更改

[英]Clicking Separate Div Causes Radio Button To Change

I'm having a problem where clicking anywhere inside a separate div is causing the radio button to change to the first radio button.我遇到了一个问题,即单击单独 div 内的任意位置会导致单选按钮更改为第一个单选按钮。 I don't know why this is happening as the panel div for adding payments is completely separated from the radio label.我不知道为什么会发生这种情况,因为用于添加付款的面板 div 与收音机 label 完全分离。

Basically, I have a bunch of cards in a list, clicking the radio button selects the cards.基本上,我在列表中有一堆卡片,单击单选按钮选择卡片。 When I try to fill out the fields to add a payment method, clicking anything within the box causes the radio button to go back to the first card.当我尝试填写字段以添加付款方式时,单击框中的任何内容都会导致单选按钮 go 返回第一张卡。 How is the handleRadioChange method propagating to the panel div? handleRadioChange 方法是如何传播到面板 div 的?

<div id="payment_methods" style={{margin: '10px'}}>
          <div className="group">
            
              {methods.map((method, key) => {

                return (
                  <label>
                    <span>
                      <input type="radio"
                      name={method.id}
                      value="saved-card-1"
                      style={{verticalAlign: 'middle', margin: '0px'}}
                      checked={radio == method.id}
                      onChange={handleRadioChange}
                      />
                    </span>
                  </label>    
                )      
              })}
              <label>
                <span>
                  <input type="radio"
                  name="add-card"
                  value="add-card"
                  style={{verticalAlign: 'middle', margin: '0px'}}
                  checked={radio == 'add-card'}
                  onChange={handleRadioChange}
                  /> 
                </span>
                  <div className="box-root hideIfEmpty">
                    <div className="box-root">
                      <div className="box-root flex flex-row flex-justify-flexstart flex-wrap">
                          <div className="flex flex-align-baseline flex-row">
                            <div className="flex align-baseline flex-row flex-justify-flexstart">
                              <span className="button-link-label text-proportional text-typeface text-wrap-nowrap text-gray">
                                <span> Add Payment Method </span>
                              </span>
                            </div>
                          </div>
                      </div>
                    </div>
                  </div>
              </label>
            </div>
           </div>


        <CSSTransition
          classNames="method"
          in={radio === 'add-card'}
          timeout={100}
          unmountOnExit>
            <div className="panel panel-primary">
        </CSSTransition>

This is my handleRadioChange method这是我的 handleRadioChange 方法

    const handleRadioChange = async (e) => {
    console.log(e.target);

    setRadio(e.target.name)

    if (e.target.name == 'add-card') {
        console.log('Creating payment fields')
    }

}

The event on your input is propagating to the label, which contains everything else inside your card.您输入的事件正在传播到 label,其中包含卡内的所有其他内容。 Try adding e.preventDefault() to your handler, or moving non-input elements outside of your label element.尝试将 e.preventDefault() 添加到您的处理程序,或将非输入元素移到您的 label 元素之外。 You're also using a unique name attribute for each payment method in your array.您还为数组中的每种付款方式使用了唯一的名称属性。 That would prevent them from acting as a single radio group.这将阻止他们充当一个单一的广播组。

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

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