/ / Como alterar o valor do link de “Escolher” para “Escolhido”? - javascript, jquery, hyperlink

Como alterar o valor do link de "Escolher" para "Escolhido"? - javascript, jquery, hyperlink

Aqui está minha entrada:

<a id="choose" alt="554">Choose</a>

Como posso obter o resultado depois de clicar no link "Escolher", tornar-se "Escolhido" e depois voltar para "Escolher" depois de clicar nele novamente?

Minha saída esperada:

<a id="choose" alt="554">Choosen</a>

Aqui está o meu Ajax:

   $(function(){

$("#choose").live("click",function(){
var $this = $(this);
var chapter_id = $this.attr("alt");

if($this.hasClass("edu_level_active")){
del_chapter(chapter_id,$this);
}else{
if($this.hasClass("disabled")){
return false;
}else{
ins_chapter(chapter_id,$this);
}
}

});

});

function ins_chapter(chapter_id,$this)
{
$.ajax({
type      : "post",
url       : "/dashboard/choose/",
data      : "chapter_id:"+chapter_id,
beforeSend: function(){
$this.addClass("edu_level_active");
},error   : function(){
alert("error");
}
});

return false;
}

Respostas:

1 para resposta № 1

Você pode definir uma variável booleana que indica um clique ou não.

var retVal = false
$("#choose").on("click", function(){
if (!retVal){
retVal = true;
$(this).text("Choosen");
}
else {
retVal = false;
$(this).text("Choose");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<a id="choose" alt="554">Choose</a>


0 para resposta № 2

É simples, basta testar o valor atual do texto e alterá-lo de acordo:

$("#choose").live("click",function(){
var $this = $(this);
var chapter_id = $this.attr("alt");
var chapter_text = $this.text().trim();

if (chapter_text === "Chosen") {
$this.text("Choose");
} else if (chapter_text === "Choose") {
$this.text("Chosen");
}

if ($this.hasClass("edu_level_active")) {
del_chapter(chapter_id,$this);
} else{
if ($this.hasClass("disabled")) {
return false;
} else{
ins_chapter(chapter_id,$this);
}
}
});

0 para resposta № 3

A partir do jQuery 1.7, o .viver() método é obsoleto. Usar .em() para anexar manipuladores de eventos. Experimente isto :.

$("#choose").on("click",function(){
var $this = $(this);
var chapter_id = $this.attr("alt");
var a_text = $this.text().trim();

if(a_text=="Choose"){
$this.text("Choosen");
}
else{
$this.text("Choose");
}


if($this.hasClass("edu_level_active")){
del_chapter(chapter_id,$this);
}else{
if($this.hasClass("disabled")){
return false;
}else{
ins_chapter(chapter_id,$this);
}
}

});