Refresh page after onclick in

2019-01-26 22:15发布

问题:

I have the following code:

<a onclick="removeCart('<?php echo $product['key']; ?>');" name="remove[<?php echo $product['key']; ?>]"  class="button"><span>Remove</span></a>

When I click the "Remove" button, which is shown with the above code, I want the "onclick" to be executed (which is executed), and after that, the page to be refreshed. Any thoughts?

Thanks.

回答1:

To reload the page using javascript you can do:

window.location.href=window.location.href

So to reload after the onclick simply add that to the end of onclick, eg:

onclick="removeCart('<?php echo $product['key']; ?>');window.location.href=window.location.href;"

edit: full code with delay

<a onclick="removeCart('<?php echo $product['key']; ?>');setTimeout('window.location.href=window.location.href', 100)" name="remove[<?php echo $product['key']; ?>]"  class="button"><span>Remove</span></a>


回答2:

First answer is correct, also you could use

 window.location.reload();


回答3:

you can resubmit the page to itself by triggering the submit of an input field for instance. Assuming that your hyperlink got the id nextLink and you got an input with id submitMe you can do :


 $("#nextLink").click(function () {
//setting the url of the action target (your page or controller) $('#submitMe').attr('action', '/MySection/MyPage.php'); $("#submitMe").submit(); });



标签: refresh