I have a vertical unorder list of 5 radio buttons, with dynamic text generated every 2 minutes.
我有一个5个单选按钮的垂直无序列表,每2分钟生成一次动态文本。
<ul id="answers_ul">
<li><input class="alternative-letter" id="r1" name="r1" type="radio"><div class="answers_letters">A</div><label class="res1" for="r1"></label><svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"></svg></li>
<li><input class="alternative-letter" id="r2" name="r1" type="radio"><div class="answers_letters">B</div><label class="res2" for="r2"></label><svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"></svg></li>
<li><input class="alternative-letter" id="r3" name="r1" type="radio"><div class="answers_letters">C</div><label class="res3" for="r3"></label><svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"></svg></li>
<li><input class="alternative-letter" id="r4" name="r1" type="radio"><div class="answers_letters">D</div><label class="res4" for="r5"></label><svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"></svg></li>
<li><input class="alternative-letter" id="r5" name="r1" type="radio"><div class="answers_letters">E</div><label class="res5" for="r5"></label><svg viewBox="0 0 100 100" xmlns="http://www.w3.org/2000/svg"></svg></li>
</ul>
The problem is the following. I add a button below the
问题如下。我在下方添加了一个按钮
<button class="clean_question"></button>
As the class is named, the function of the button is to clear the answers of the radio button (if checked). But when I clicked it, the page load the previous page in history! That is really crazy.
在命名类时,按钮的功能是清除单选按钮的答案(如果选中)。但是当我点击它时,页面会加载历史记录中的上一页!那太疯狂了。
I will paste the JS and CSS that is related to the clean_question button
我将粘贴与clean_question按钮相关的JS和CSS
CSS
button.clean_question{
background-color: #292f33;
border: 1px solid #55acee;
width: 60px;
height: auto;
margin-top: 5px;
border-radius: 50%;
margin-left: -34px;
float: left;
padding: 10px 12px;
z-index: 100;
position: absolute;
transition: 0.3s;
-webkit-transition: 0.3s;
}
button.clean_question:hover{
transform: scale(1.1,1.1);
}
button.clean_question:before{
content: url(../images/Examen_Botones_Clean.svg);
}
and the JS
和JS
function resetRadio(el) {
[].slice.call($('input[type="radio"][name="' + el.getAttribute('name') + '"]')).forEach(function(el) {
var path = el.parentNode.querySelector('svg > path');
if (path) {
path.parentNode.removeChild(path);
}
});
}
jQuery('button.clean_question').on('click', function(){
console.log('llegaste');
selectsArr.forEach(function(obj,i) {
[].forEach.call(obj.inputs,function(el){
if (el.getAttribute('type') === 'radio')
resetRadio(el);
})
});
Please try yo help me, I'm really stuck with this problem.
请尝试哟帮助我,我真的坚持这个问题。
1 个解决方案
#1
0
<button type="">
attribute defaults to submit
.
https://www.w3.org/TR/html/forms.html#attr-button-type
So clicking it will submit the form by default. Form target is by default the same page, which results in reloading. Use <button type="button">
to avoid that.
因此,单击它将默认提交表单。默认情况下,表单目标是同一页面,这会导致重新加载。使用
#1
0
<button type="">
attribute defaults to submit
.
https://www.w3.org/TR/html/forms.html#attr-button-type
So clicking it will submit the form by default. Form target is by default the same page, which results in reloading. Use <button type="button">
to avoid that.
因此,单击它将默认提交表单。默认情况下,表单目标是同一页面,这会导致重新加载。使用