开发者

Pass in 2 parameters for a jquery function

开发者 https://www.devze.com 2022-12-20 06:02 出处:网络
I have this function that works, but I would like to make this one as a general function so I can pass in 2 variables.

I have this function that works, but I would like to make this one as a general function so I can pass in 2 variables.

<script type="text/javascript">
       $(function() {
           $(SOME_ID_HERE).click(function() {
               if (confirm(SOME_ERRORMESSAGE_HERE)) {
                   $.post(this.href, function(data) {
                       document.location.reload();开发者_Python百科
                   });
                   return false;
               }
           });

       }); 
</script>

How shall I modify this one so I can pass in SOME_ID_HERE and SOME_ERRORMESSAGE_HERE? and how do I call that function?

/M


You can do:

function reload(id, message) {
  $(id).click(function() {
     if (confirm(message)) {
         $.post(this.href, function(data) {
             document.location.reload();
         });
         return false;
     }
  });
}

Call:

reload("#id5", "Are you sure?");

but I can't imagine where you'd want this, you have a scenario?


Try this:

$(function() {
    myFunction("#id", "Error message");
});

function myFunction(id, msg) {
    $(id).click(function() {
        if (confirm(msg)) {
            $.post(this.href, function(data) {
                document.location.reload();
            });
            return false;
        }
    });
}
0

精彩评论

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