When selecting "A&L" in the select, the radio group is hidden and its value is set to "n".
(在选择中选择“ A&L”时,单选按钮组被隐藏,其值设置为“ n”。)
I try to trigger the change event so that the "Hello"-div disappears too, but it doesn't work correctly - on debugging I noticed that the change event is executed twice - the first time correctly and then again with the value "j".(我尝试触发更改事件,以便“ Hello” -div也消失,但是它无法正常工作-在调试时,我注意到更改事件执行了两次-第一次正确执行,然后再次使用值“ j ”。)
What's my mistake?
(我怎么了)
Here's the full code: https://jsfiddle.net/95Lxroqy/
(这是完整的代码: https : //jsfiddle.net/95Lxroqy/)
After I looked through some other questions it seemed to me that .val(['n']).change();
(看完其他一些问题之后,我觉得.val(['n']).change();
)
(line 24) should have worked -((第24行)应该有效-)
but it seems like I'm still missing something.(但似乎我仍然缺少一些东西。)
// find elements var banner = $("#banner-message"); var button = $("#place"); var langs = $("#langs"); var trans = $("#trans"); var radioGroup = $("input[type=radio][name=translate]"); var div = $("#dynamic"); radioGroup.change(function() { if (this.value === 'n') { div.hide(); } else if (this.value === 'j') { div.show(); } }); // handle click and add class button.change(function(event){ var al = button.val() === "al"; if(al){ langs.show(); trans.hide(); radioGroup.val(['n']).change(); }else{ trans.show(); langs.hide(); } }).change();
body { background: #20262E; padding: 20px; font-family: Helvetica; } #banner-message { background: #fff; border-radius: 4px; padding: 20px; font-size: 25px; text-align: center; transition: all 0.2s; margin: 0 auto; width: 300px; } button { background: #0084ff; border: none; border-radius: 5px; padding: 8px 14px; font-size: 15px; color: #fff; } #banner-message.alt { background: #0084ff; color: #fff; margin-top: 40px; width: 200px; } #banner-message.alt button { background: #fff; color: #000; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="banner-message"> <select id="place"> <option value="in">Internal</option> <option value="al">A&L</option> </select> <select id="langs"> <option value="volvo">German</option> <option value="saab">English</option> </select> <fieldset id="trans"> <input type="radio" id="n" name="translate" value="n"> <label for="n"> Nein</label> <input type="radio" id="j" name="translate" value="j"> <label for="j"> Ja</label> </fieldset> <div id="dynamic"> Hello </div> </div>
ask by Cold_Class translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…