Display confirmation popup with JavaScript upon clicking on a link

前端 未结 4 1916

How do I make one of those hyperlinks where when you click it, it will display a popup asking \"are you sure?\"



        
4条回答
  •  独厮守ぢ
    2021-01-17 16:45

    As Nahom said, except I would put the javascript:message() call directly in the href part (no need for onclik then).

    Note: leaving the JavaScript call in the onClick has a benefit: in the href attribute, you can put a URL to go to if the user doesn't have JavaScript enabled. That way, if they do have JS, your code gets run. If they don't, they go somewhere where they are instructed to enable it (perhaps).

    Now, your message routine must not only ask the question, but also use the answer: if positive, it must call submit() on the form to post the form. You can pass this in the call to ease the fetching of the form.

    Personally, I would go for a button (input tag as you show) instead of a simple link to do the process: it would use a more familiar paradigm for the users.

    [EDIT] Since I prefer to verify answers I give, I wrote a simple test:

    
    
    

    Yes, the submit just reload the page here... Tested only in FF3.

    [EDIT] Followed suggestion in the comments... :-)

提交回复
热议问题