D
D
DestroyerEvo2021-08-02 11:14:21
AJAX
DestroyerEvo, 2021-08-02 11:14:21

How to pass value to window.location?

There is a list of models. By clicking on the model, a form opens, after submitting the form, the file is automatically downloaded. This moment works (with manually written window.location), but I need to click on the model, pass the value from the link attribute a href="" to window.location

$(document).on('submit','#ajaxForm form',function(ev){
var frm = $('#ajaxForm form');
$.ajax({
    type: 'post',
    url: '/form6',
    data: frm.serialize(),
    success: function (data) {
   $('#ajaxForm  form').remove();
     $('#ajaxForm').html( data );
  window.location= '';	
    }
});
ev.preventDefault();
})

    
      
      $(document).ready(function(){
        
$('a.mod').click(function() {
$(".par1")
var sup = ($(this).attr('title2'));
    $(".par1").html(sup);
    
  
});
        
  });


<div class="model">
   <a href="#modal" title2="http://ссылка на файл">Название модели</a>
</div>


You need to pass the title2 attribute. How to do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DestroyerEvo, 2021-08-02
@DestroyerEvo

It turned out like this:

$(document).ready(function(){

$('a.mod').click(function() {
$(".par1")
var sup = ($(this).attr('title2'));
$('#ajaxForm').attr('title3', sup)
  
});
        
  });			
    

    $(document).on('submit','#ajaxForm form',function(ev){
var frm = $('#ajaxForm form');
$.ajax({
    type: 'post',
    url: '/form6',
    data: frm.serialize(),
    success: function (data) {
   $('#ajaxForm  form').remove();
     $('#ajaxForm').html( data );
   var sup2 = $('#ajaxForm').attr('title3');	
   window.location= sup2;		
    }
});
ev.preventDefault();
})

At the beginning I pass an attribute with a link to the file to the form wrapper, then I pass this attribute to windows.location
Everything works

S
Stalker_RED, 2021-08-02
@Stalker_RED

document.addEventListener('click', linksHandler); // слушаем все клики

function linksHandler(evt) {
  // проверяем, были ли клик по ссылке с title2 или по элементу внутри такой ссылки
  const link = evt.target.closest('[title2]');
  if (link) {
    const title2 = link.getAttribute('title2');
    console.log(link);
    console.log(title2);
    // передавай что хочешь
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question