douji8347 2013-03-01 23:12
浏览 270
已采纳

JSON - 使用JS在Json中搜索String的值

I need a simple way to search my Json string, using javascript.

Here's my PHP that creates my Json String:

<?php
$allnames = array();
$res = mysql_query("SELECT first,last FROM `tbl_names`");
while ($row = mysql_fetch_array($res)){
  $allnames[$i++] = $row['first'].':'.$row['last'];
}

echo $jsonstring = json_encode($allnames);
/* 
["john:smith","tony:stark","bruce:banner","clark:kent"]
*/
?>

I intend to put that $jsonstring into a cookie, so I can reference it on several different pages, saving me from making any future queries. I'm using the jquery cookie plugin from: https://github.com/carhartl/jquery-cookie

<script type="text/javascript" src="jquery.cookie.js"></script>
<script language="javascript" type="text/javascript">
$(document).ready(function(){
   $.cookie("allNames", JSON.stringify(<?=$jsonstring;?>))
});
</script>

So far so good! The cookie exists, and the data is saved, I can see in the browser.

I'm now interesting in searching that cookie's value, for instances of any of those names. And if I find one, I'll have options to perform, depending on my pages.

What I'd like to do is perform an onkeyup event, from a text box on a page:

<input type="text" name="lastname" id="lastname" />
<script language="javascript" type="text/javascript">
$(document).ready(function(){

  $("#lastname").keyup(function() {
    // search the "allNames" cookie value for lastname
     var allNames = $.cookie("allNames"); // gets cookie
     var lastname = $(this).val();

     // this is not seeming to work:
     if( allNames.text().search('stark') != -1){
        alert("that name exists");
     }else{
    alert("name does not exist");   
     }

  });

});
</script>

I'm sure it's an easy task that I'm just not grasping. And maybe json isn't the best way to save cookie data either.

But how would I search a cookie's value? Or can someone advise a better solution?

  • 写回答

3条回答 默认 最新

  • dongruolin5324 2013-03-01 23:21
    关注

    Here's how I'd do it:

    First create the JSON and store in cookie:

    <?php
        $allnames = array();
        $res = mysql_query("SELECT first,last FROM `tbl_names`");
    
        while ($row = mysql_fetch_array($res)){
            $allnames[$row['last']] = $row['first'];
        }
    
        setcookie("allNames", json_encode($allnames), time()+(3600*24*10)); //10 days
    ?>
    

    Then get it :

    $(function(){
        $("#lastname").on('keyup', function() {
            var allNames = JSON.parse($.cookie("allNames")); // gets cookie
            var lastname = this.value.trim();
    
            if (lastname in allNames) {
                alert("that name exists");
            }else{
                alert("name does not exist");
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作