简体   繁体   English

如果其他按钮具有相同的文本,则单击单选按钮会更改其他按钮的样式

[英]Clicking on a radio button changes other buttons styling if they have the same text

I am doing a React Quiz based on the OpenTrivia Api, I can't figure out an way to make that only the button that you click changes it's styling, for example if there are multiple true or false questions, if I click, all the other question answers will also change the styling.我正在做一个基于 OpenTrivia Api 的 React 测验,我想不出一种方法来让你点击的按钮改变它的样式,例如,如果有多个正确或错误的问题,如果我点击,所有其他问题的答案也会改变样式。 That doesn't happen with any other questions, only if the answers have the exact same text, so a true or false or multiple color option for example.任何其他问题都不会发生这种情况,只有当答案具有完全相同的文本时,例如正确或错误或多种颜色选项。 I've set the opacity of the radio button to zero.我已将单选按钮的不透明度设置为零。

例子

This is the React code这是反应代码

function answerStyle(str) {
        if (props.gameState == "finished") {
            if (str == props.correctAnswer) {
                return "correct-markedAnswer";
            }        
        }
    }

const answersElem = answerArr.map(answer => (
        <div key={answer}>
            <input 
                type="radio" 
                id={`${props.question} ${answer}`} 
                name={props.question} 
                value={answer}
                checked={props.markedAnswers.includes(answer)} 
                onChange={props.onChange}/>
            <label htmlFor={`${props.question} ${answer}`} className={`${answerStyle(answer)}`}>{fixText(answer)}</label>
        </div>))

CSS Code CSS 代码

input[type="radio"] {
    opacity: 0;
    margin: 10px;
    margin-bottom: 25px;
}

label {
    border-width: 2px;
    border-style: solid;
    border-radius: 10px;
    border-color: #2187ab;
    padding: 10px;
    padding-left: 30px;
    padding-right: 30px;
    margin-bottom: 5px;
}

input[type="radio"]:checked + label {
    border-width: 2px;
    border-style: solid;
    border-radius: 10px;
    padding: 10px;
    border-color: #2187ab;
    background-color: #2187ab;
    color: white;
    padding-left: 30px;
    padding-right: 30px;
}
.correct-markedAnswer {
    border-width: 2px;
    border-style: solid;
    border-radius: 10px;
    padding: 10px;
    border-color: #2eb82e;
    background-color: #2eb82e;
    color: white;
    padding-left: 30px;
    padding-right: 30px;
}

add a unique key添加唯一键

 const answersElem = answerArr.map(answer => ( <div key={`${props.question} ${answer}`}> <input type="radio" id={`${props.question} ${answer}`} name={props.question} value={answer} checked={props.markedAnswers.includes(answer)} onChange={props.onChange}/> <label htmlFor={`${props.question} ${answer}`} className={`${answerStyle(answer)}`}>{fixText(answer)}</label> </div>))

The problem was with the checked value in this, removing it solved the problem.问题在于其中的检查值,删除它解决了问题。

        <div key={`${props.question} ${answer}`}>
            <input 
                type="radio" 
                id={`${props.question} ${answer}`} 
                name={props.question} 
                value={answer}
                checked={props.markedAnswers.includes(answer)} 
                onChange={props.onChange}/>
            <label htmlFor={`${props.question} ${answer}`} className={`${answerStyle(answer)}`}>{fixText(answer)}</label>
        </div>))

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

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