Atualize o botão HREF do link através do jQuery
-
21-09-2019 - |
Pergunta
Estou usando um botão de link para redirecionar a página para o local desejado com algum valor de sequência de consulta do jQuery. O código do botão de link é o seguinte:
<td>
<a id="selectAllLink" class="button" rel="nofollow ibox&width=800&height=400&title=Contact Now"
href="#" onclick="return (this.href=='#');">Contact Selected</a>
</td>
E o jQuery que criará/atualizará o link no evento Click do meu botão de link são os seguintes:
function CotactSelected() {
var a = [];
var n = $("td.title_listing input:checked");
var s = "";
n.each(function() {
a.push($(this).val());
});
var s = a.join(',');
if (s != null) {
$("@.button#selectAllLink").attr("href", "/D_ContactSeller.aspx?property=" + s);
}
else {
alert("Select atleast one property to contact!");
}
}
O que eu queria fazer é coletar todo o valor separado da vírgula das caixas de seleção e passará para a outra página com esse valor coletado como string de consulta. Ao clicar nesse botão de link, ele deve transportar todos os valores separados da vírgula e redirecionado para a página desejada. Por favor, me ajude .. obrigado antecipadamente.
Solução
Use isso em vez de sua função CotactSelected
$(function() {
$('#selectAllLink').each(function() {
var a = [];
var n = $("td.title_listing input:checked");
var s = "";
n.each(function() {
a.push(this.value);
});
s = a.join(',');
if (a.length > 0)
this.href= "/D_ContactSeller.aspx?property=" + s;
else
this.href = 'javascript:alert("Select at least one property to contact!");';
return false;
});
});
Outras dicas
if (s != null) {
$("@.button#selectAllLink").attr("href", "");
$("@.button#selectAllLink").attr("href", "/D_ContactSeller.aspx?property=" + s);
}
else {
alert("Select atleast one property to contact!");
}
Espero que isto ajude :)
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow