开发者

How to call ActionMethod from javascript?

开发者 https://www.devze.com 2023-04-12 20:31 出处:网络
I have a button that prompts user about his action(deleting smth). If user confirms, I want to execute ActionMethod with postback. How ca开发者_高级运维n i achieve that?Yes that is simple, you can do

I have a button that prompts user about his action(deleting smth). If user confirms, I want to execute ActionMethod with postback. How ca开发者_高级运维n i achieve that?


Yes that is simple, you can do it this way

JS

$('#id-of-your-button').click(function() {
    if(confirm('Are you sure'))
         document.location = '/controller/action/id_to_delete';
});

or with the postback

$('#id-of-your-button').click(function() {
  if(!confirm('Are you sure'))
       return false;
});

if your button is not submit button than you can do it this way

$('#id-of-your-button').click(function() {
      if(confirm('Are you sure'))
           $('#id-of-your-form').submit();
      return false;
    });

if you don't have anything (form and submit button)

$('#id-of-your-button').click(function(){
  $.ajax({ url: '/controller/action', 
    dataType: 'html', 
    data: { id_to_delete: $('#where_are_you_holding_your_value').val() },
    type: 'post', 
    success: function(data) {
       alert('your item is deleted');
    }
});
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号