Ajax формирование урла с переменными
Пытаюсь сделать вот такое:
<span onClick="sendi(this.id,this.type)" id="2" type="h1">Смотреть</span>
function sendi(clicked_id,clicked_type)
{
var $linked = '/path?' + clicked_type + '&id=' + clicked_id;
$.ajax({
url: $linked,
cache: false,
success: function(html){
$("#cont").html(html);
}
});
}
Т.е. внутрь урла для ajax мне нужно добавить две переменные (id и type) - упорно выдает ошибку unexpected string
Ответы (1 шт):
Автор решения: InDevX
→ Ссылка
Если вы добавили кастомный атрибут то его можно получить используя метод getAttribute()
function sendi(id, type)
{
let $link = '/path?' + type + '&id=' + id;
console.log('id => ', id);
console.log('type => ', type);
$.ajax({
url: $link,
cache: false,
success: function(html){
$("#cont").html(html);
}
});
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span onClick="sendi(this.id, this.getAttribute('type'))" id="2" type="h1">Смотреть</span>