2017-03-11 3 views
0

Ich habe ein <li> Element, in dem Text von einem Controller angehängt wird. Wenn der Text lautet: "E-Mail existiert bereits." Ich möchte sonst einen Link anhängen, ich möchte nichts anhängen.Ermitteln, ob ein li-Element einen bestimmten Text enthält

Ich habe die folgende Funktion in jQuery, aber es funktioniert einfach nicht:

if ($("#validat ul li").indexOf("Email") >= 0) 
{ 
    $("#validat ul li").append("<a href='@Url.Action("ForgotPassword","Auth")'>Forgot password?</a>"); 
} 

Was bin ich?

+1

$ ("# VALIDAT ul li") könnte eine Sammlung sein. Sie müssen sagen, welche. Dann benutze .text(), um den Text davon zu erhalten - oder benutze einfach li: contains – mplungjan

Antwort

0

Verwenden Sie .each() Methode. Details werden in Snippet kommentiert.

SNIPPET

/* Each list item do this... */ 
 
$('#validat ul li').each(function() { 
 

 
    /* If the <li> has text content of 
 
    || "You got Mail"... 
 
    */ 
 
    if ($(this).text() === "You got Mail") { 
 
    /* append() a link within that <li> is 
 
    || the last child 
 
    */ 
 
    $(this).append("<a href='#somewhere'>Click Here</a>"); 
 
    } 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="validat"> 
 
    <ul> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li></li> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li></li> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li></li> 
 
    <li>You got Mail</li> 
 
    <li></li> 
 
    <li></li> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li></li> 
 
    <li></li> 
 
    <li></li> 
 
    <li></li> 
 
    <li>You got Mail</li> 
 
    <li>You got Mail</li> 
 
    <li></li> 
 
    <li>You got Mail</li> 
 
    </ul>

+0

Genau! Danke :-) –

+0

@JeppeChristensen Großartig! Du bist herzlich Willkommen! Glückliche Kodierung. – zer00ne

Verwandte Themen