Remove button default function and assign another button that allows you to go back to the previous page

0

I have a button that should return me to the previous page, however, it sends me to a submit, function that has another button and no idea, it has it too. I want to remove the function by default and assign it to return to the previous page with an onclick. I'm doing it this way but simply the button does not do anything anymore.

    $('.regresar').on('click',function(e){
        e.preventDefault();
        history.back();
    });
    
asked by Kinafune 26.08.2018 в 20:02
source

1 answer

1

The buttons do not have 'default action'. Although they may cause a form submission if it belongs to a form and is marked with attribute type="submit" instead of type="button"

Here is a small example of a form with two buttons, one marked as submit and another as button . Both will send the form. One of them natively, and another programmatically.

We also take the opportunity to see the operation of preventDefault , which in this case applies to prevent the sending of the form by the traditional method.

(function() {

	var form = document.getElementById('myform');
	function onSubmit(event) {
      // Prevenimos el envio del formulario.
	    if (event) { event.preventDefault(); }
	    console.log('submitted');
	}
	form.addEventListener('submit', onSubmit, false);
	form.submit = onSubmit;       // Notese que obSubmit queda anclado a form.submit, no a button.click
	console.log(urlEncodeFormData(form));

	function urlEncodeFormData(form) {
	    var i, e, data = [];
	    for (i = 0; i < form.elements.length; i++) {
	        e = form.elements[i];
	        if (e.type !== 'button' && e.type !== 'submit') {
	        	data.push(encodeURIComponent(e.id) + '=' + encodeURIComponent(e.value)); 
	        }
	    };
	    return data.join('&');
	}

})();
<form id="myform">
    Name: <input id="name" type="text" value="dperezv" /><br />
    Email: <input id="email" type="text" value="[email protected]" /><br />
    <br />
    <button type="submit">type=submit</button>
    <button type="button" onclick="form.submit()">form.submit()</button>
</form>
    
answered by 26.08.2018 / 20:29
source