Answer the question
In order to leave comments, you need to log in
How to make ajax click on a random number of links on the page?
Links on the page are displayed in my php code like this
<?php foreach ($ids as $itemIds): ?>
<a id="url-1" href="/myscript.php?id=<?php echo $itemIds ?> ?>">Клик по ссылке 1</a>
<div id="info-1"></div>
<br>
<a id="url-2" href="/myscript.php?id=<?php echo $itemIds ?> ?>">Клик по ссылке 2</a>
<div id="info-2"></div>
<br>
<a id="url-3" href="/myscript.php?id=<?php echo $itemIds ?> ?>">Клик по ссылке 3</a>
<div id="info-3"></div>
<?php endforeach; ?>
<div id="info-1"></div>
<script>
function funcBefore(){
$("#info-1").text("Ожидание данных");
}
function funcSuccess(data){
$("#info-1").text(data);
}
$(document).ready(function(){
$("#url-1").on("click", function(){
$.ajax({
url: "myscript.php",
type: "GET",
dataType: "html",
beforeSend: funcBefore,
success: funcSuccess
});
});
});
</script>
Answer the question
In order to leave comments, you need to log in
Add some class to all links that should be processed, and instead of the id selector, use the class selector in jq. Get the parameters for the get request from the link itself using the same jQuery, create the query string dynamically after the click.
things can be simpler:
$('[data-toggle="myloader"]').on('click',function(e){
e.preventDefault(); //отменили get
var url = $(this).attr('href'), // взяли ссылку
container_selector = $(this).attr('data-container'); // взяли селектор
$(container_selector).load(url); //отправили ajax, получили html вставили в контейнер
});
<a href="/some/url/to/?hrenEgoZnaet=kuda" data-container="#some_id" data-toggle="myloader">жмакай нежно</a>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question