简体   繁体   English

如何使用Javascript将Json与字符串匹配

[英]How to Match Json with string using Javascript

What I need to do: 我需要做什么:

  • I need to match Json with string . 我需要将Json与string匹配。

case 1: When industry data is set in Json 情况1:在Json中设置了行业数据时

        ["", "app.php", "automotive", "tradeshows"]

JQuery code: jQuery代码:

  $( document ).ready(function() 
 {


    var match =  window.location.pathname.split("/");
    console.log(match[3]);

    if(match[3]=="conferences")
    {
         console.log("true");
    $('#radio3').prop('checked', true);

    }
    else if(match[3]=="tradeshows")
    {
        console.log("afeef");
        $('#radio2').prop('checked', true);
    }
    else
    {
        $('#radio1').prop('checked', true);
    }



   });

case 2: When industry data is not set in Json 情况2:未在Json中设置行业数据时

     ["", "app.php",  "tradeshows"]
  • In this case match would be failed. 在这种情况下,匹配将失败。

  • Is there any other way in match Json with string in JQuery? 在JQuery中将Json与string匹配还有其他方法吗?

  • I need to match the Json with string so is there any other way so that I could match Json with string ? 我需要将Json与string匹配,是否还有其他方法可以使Json与string匹配?

You can use jQuery indexOf() to detect if automotive is available or not. 您可以使用jQuery indexOf()来检测automotive是否可用。 See the below code 见下面的代码

 function myFunction(page) { var pages = ["", "app.php", "automotive", "tradeshows"]; var a = pages.indexOf(page); document.getElementById("demo").innerHTML = a; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <p>Click the button to display the position of the element "tradeshows":</p> <button onclick="myFunction('automotive')">Check for automotive</button> <button onclick="myFunction('automotive1')">Check for automotive1</button> <p id="demo"></p> 

Note: The indexOf method was introduced in JavaScript 1.6, and does not work in Internet Explorer 8 and earlier versions. 注意: indexOf方法是JavaScript 1.6中引入的,在Internet Explorer 8和更早版本中不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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