简体   繁体   English

单击按钮时页面返回历史记录

[英]Page go back in history when button clicked

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 的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. 请尝试帮助我,我真的被这个问题困扰了。

<button type=""> attribute defaults to submit . <button type="">属性默认为submit

https://www.w3.org/TR/html/forms.html#attr-button-type 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. 使用<button type="button">可以避免这种情况。

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

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