开发者

Regex-ing text in a textfield to be safe for URLs being loaded

You can demo this searching for the tag

Black & White

at http://syndex.me

The logo of the site is a search field. If I search for "USA" it ajax loads all posts tagged "USA". However it does not work for "Black & White". How can one check the value of the input field for potential non alphabetic characters and things like spaces, and then change it too a safe term, i.e.

Black_%26_White

?

I know a little code but this kinda stuff is beyond me. I'm sure this question would be useful for other noobs. Thanks!

    <html>
    <head>
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script type="text/javascript">
    jQuery(function($) {
    $('#syndex').keydown(function(event) {
      if (event.keyCode == '13') {
        var syndex = $('input:text').val();
        $('body').load("http://syndex.me/tagged/"+开发者_开发百科 syndex,function(){
        $('input:text').val(syndex);
    });
       }        
    });
    });
    </script>
    </head>
        <body>
        <div id="logo">
            <input id="syndex" type="text"/>
        </div>
        </body>
    </html>


Do you want to strip everything except letters and numbers?

var syndex = $('input:text').val().replace(/\W\D/g, '');

Or do you want to convert special characters to a URI-friendly format?

$('body').load("http://syndex.me/tagged/"+ encodeURIComponent(syndex),function(){
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜