简体   繁体   中英

Passing value from one input to keyup function using .filter

I am trying to filter the form value to change some links color and background but this is incorrect

$("a").filter('a[href$='"'+username'"]').css("background-color", "#ff0000");   

Here is the HTML example

 <body>
    <div class="main"> 
    <p> 
    Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    <a href="u=3153">user1</a>, <a href="u=31532">user2</a>, <a href="u=231532">user3</a>, <a href="u=11111">user4</a>, <a href="u=22222">user5</a>.
    </p> 
    </div>
    <div id="buttons">
    <form method="post" action="">
    Username:<br /> 
    <input type="text" value="" class="userform"  id="user" maxlength="15"/>
    <br />
    <input type="submit" value="   Submit   "  class="submit"/>
    </div>
    </body>   

And this is the jquery

$(document).ready(function(){
    $("body").keyup(function() 
    {
    var username = $("#user").val();
    $("a").filter('a[href$='"'+username'"]').css("background-color", "#ff0000");
    });
    });

My problem is that I can't "put" the form value in the keyup function

You have a syntax error in your selector, try this:

 $("a").filter('a[href$="'+username+'"]').css("background-color", "#ff0000");
            //('a[href$='"'+username'"]')                      
                 //     ^           ^

example: http://jsfiddle.net/niklasvh/Lm6gE/

You have a problem with quotes. The code should be:

$("a").filter('a[href$="'+username+'"]').css("background-color", "#ff0000");

You misplaced a " and there was a lacking + .

Hope this helps. Cheers

There is a syntax error in the filter method, should be:

$("a").filter('a[href$="'+username'"]')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM