[英]Run a function on document.ready and onClick
Is there a way to make this ajax-response run on document.ready, AND onClik without copy/paste the code in another function? 有没有一种方法可以使此ajax响应在document.ready和ANDonClik上运行,而无需在其他函数中复制/粘贴代码?
$(document).ready(function() {
var ads = $('#ads').val();
function ajax(){
$.ajax({
url: "./services/finn_bilder.php",
type:"POST",
data:{ads: ads},
success:function(data){
$('#AdsDiv').html(data);
}
I want this to run onClick $('#id').click(function()); 我希望它运行onClick $('#id')。click(function()); and the way its working now, without copy/paste?
以及它现在的工作方式,没有复制/粘贴?
Just make sure you can reuse the javascript function you've created: 只要确保您可以重复使用已创建的javascript函数:
$(document).ready(function() {
// Will accure at document load
ajax();
// Will accure at every click
$(document).click(function() { ajax(); });
}
function ajax(){
var ads = $('#ads').val();
$.ajax({
url: "./services/finn_bilder.php",
type:"POST",
data:{ads: ads},
success:function(data){
$('#AdsDiv').html(data);
});
}
Just call it inside document ready and bind it to document.click. 只需在ready文档中调用它并将其绑定到document.click。
$(document).ready(function() {
var ads = $('#ads').val();
function ajax(){
$.ajax({
url: "./services/finn_bilder.php",
type:"POST",
data:{ads: ads},
success:function(data){
$('#AdsDiv').html(data);
}
});
}
ajax();
$(document).click(function() {
ajax();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.