简体   繁体   English

将字符串化的 JSON 数据输出到单个 div 中

[英]Outputting stringified JSON data into individual div's

In this function, data from the users array is searched by the value of input into mySearch .在这个函数中,来自users数组的数据通过输入到mySearch的值进行搜索。 The line that contains the search term is outputted to return .包含搜索词的行被输出到return

DATA:数据:

var users = [{"username":"nlee6p","gender":"Female","sexuality":"Down-sized asymmetric time-frame","language":"Macedonian"},
{"username":"bbrooks6q","gender":"Male","sexuality":"Multi-channelled mobile customer loyalty","language":"Luxembourgish"},{"username":"astephens6r","email":"lmiller6r@ucoz.com","gender":"Female","language":"Estonian"}]


function myFunction(e) {
    if((e.target.id === 'mySearch' && e.keyCode === 13) || e.target.id === 'searchButton'){
        e.preventDefault();
        var searchValue = document.getElementById("mySearch").value;
        for(var i = 0; i < users.length; i++){
        if((users[i]['last_name'] === searchValue) || (users[i]['username'] === searchValue) || (users[i]['first_name'] === searchValue)){
        document.getElementById("return").innerHTML = JSON.stringify(users[i]);
        return;
       }  
    }
  }
}

HTML HTML

<input type="search" id="mySearch"  onkeypress="myFunction(event)"/>
<button id="searchButton" onClick="myFunction(event)">SEARCH</button>

<div id="return">
    <div id="usernameOut">
    </div>
    <div id="firstNameOut"> 
    </div>
</div><!--return-->

My Question我的问题

Is it possible to output the stringified data in individual divs without jQuery?是否可以在没有 jQuery 的情况下在单个 div 中输出字符串化数据? Currently the output is the entire line of the array relating to the searched term.目前,输出是与搜索词相关的数组的整行。 I'm looking for a way to split the line up and display the individual components in their own div tags (or in a table if that's easier)我正在寻找一种方法来拆分行并在他们自己的 div 标签中显示各个组件(或者在表格中,如果这更容易的话)

For example, When the string is outputted, the username value will be shown in usernameOut , first-name in firstNameOut etc.例如,当字符串输出时, username值将显示在usernameOutfirst-namefirstNameOut等中。

I've used Jput in the past, but this is for a page that's being displayed locally through a C# application and compiling jQuery in C# is a bit of a nightmare.我过去使用过Jput ,但这是针对通过 C# 应用程序在本地显示的页面,在 C# 中编译 jQuery 有点像一场噩梦。

Answers using vanilla JS are appreciated, but if the only way is through jQuery then i'm not too fussed.使用 vanilla JS 的答案表示赞赏,但如果唯一的方法是通过 jQuery,那么我不会太大惊小怪。

JsFiddle (that doesn't work for some reason) JsFiddle(由于某种原因不起作用)

(Sorry if this is unclear, I will edit if needed) (对不起,如果这不清楚,我会根据需要进行编辑)

Here is the working fiddle:这是工作小提琴:

https://jsfiddle.net/zfywyyfj/2/ https://jsfiddle.net/zfywyyfj/2/

 no changes were made to the code, just reposition.

You set it to only show when it's completly equal, so you have to type the whole thing to make it dump the json string.您将其设置为仅在完全相等时显示,因此您必须键入整个内容以使其转储 json 字符串。

Also, i moved the JS script to the head, because otherwise it will get called before is defined.另外,我将 JS 脚本移到了头部,否则它会在定义之前被调用。 This is a Fiddle issue as pointed out by Shilly.这是 Shilly 指出的 Fiddle 问题。

UPDATE: The error is because you're trying to get last_name and first_name while the users array/object doesn't have it.更新:错误是因为您试图获取last_namefirst_name而用户数组/对象没有它。 Here is the working snippet, i also edited it to allow partials.这是工作片段,我还对其进行了编辑以允许部分。

 var users = [{"username":"nlee6p","gender":"Female","sexuality":"Down-sized asymmetric time-frame","language":"Macedonian"},{"username":"bbrooks6q","gender":"Male","sexuality":"Multi-channelled mobile customer loyalty","language":"Luxembourgish"},{"username":"astephens6r","email":"lmiller6r@ucoz.com","gender":"Female","language":"Estonian"},{"username":"rbishop84","gender":"Female","sexuality":"Centralized hybrid data-warehouse","language":"Amharic"},{"username":"wmorrison85","gender":"Male","sexuality":"Extended upward-trending throughput","language":"Aymara"}]; function myFunction(e){ if((e.target.id === 'mySearch' && e.keyCode === 13) || e.target.id === 'searchButton') { e.preventDefault(); var searchValue = document.getElementById("mySearch").value; for(var i = 0; i < users.length; i++) { if( ( users[i]['language'].includes(searchValue) ) || ( users[i]['username'].includes(searchValue) ) || ( users[i]['sexuality'].includes(searchValue) ) ) { document.getElementById("return").innerHTML = JSON.stringify(users[i]); return; } } } }
 <input type="search" id="mySearch" onkeypress="myFunction(event)"/> <button id="searchButton" onClick="myFunction(event)">SEARCH</button> <div id="return"> </div>

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

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