How to create a HTML Cancel button that redirects to a URL

前端 未结 11 1142
Happy的楠姐
Happy的楠姐 2020-12-13 13:00

I am playing with the Buttons in the w3schools Tryit editor, and I am trying to figure out how to make my browser redirect to an URL when I click on the \"Cancel\" button.

相关标签:
11条回答
  • 2020-12-13 13:38

    There is no button type="cancel" in html. You can try like this

    <a href="http://www.url.com/yourpage.php">Cancel</a>
    

    You can make it look like a button by using CSS style properties.

    0 讨论(0)
  • 2020-12-13 13:39

    it defaults to submitting a form, easiest way is to add "return false"

    <button type="cancel" onclick="window.location='http://stackoverflow.com';return false;">Cancel</button>
    
    0 讨论(0)
  • 2020-12-13 13:46

    cancel is not a valid value for a type attribute, so the button is probably defaulting to submit and continuing to submit the form. You probably mean type="button".

    (The javascript: should be removed though, while it doesn't do any harm, it is an entirely useless label)

    You don't have any button-like functionality though, so would be better off with:

    <a href="http://stackoverflow.com"> Cancel </a>
    

    … possibly with some CSS to make it look like a button.

    0 讨论(0)
  • 2020-12-13 13:47

    There is no button type cancel https://www.w3schools.com/jsref/prop_pushbutton_type.asp

    To achieve cancel functionality I used DOM history

    <button type="button" class="btn btn-primary" onclick="window.history.back();">Cancel</button>

    For more details : https://www.w3schools.com/jsref/met_his_back.asp

    0 讨论(0)
  • 2020-12-13 13:51

    With Jquery:

    $(".cancel-button").click(function (e) {
      e.preventDefault();
    });
    
    0 讨论(0)
  • 2020-12-13 13:53

    Here, i am using link in the form of button for CANCEL operation.

    <button><a href="main.html">cancel</a></button>
    
    0 讨论(0)
提交回复
热议问题