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;
}
});
}
精彩评论