简体   繁体   English

使用jquery / javascript从解析的json获取值

[英]To get value from parsed json using jquery/javascript

In the below code i am just parsing a value using json and set it to textbox and on onblur event i want to get the value of a json object.Please help me to do . 在下面的代码中,我只是使用json解析值并将其设置为文本框,并且在onblur事件上我想获取json对象的值。请帮我做。

var Reg = JSON.parse(RegularExpression);
//RegularExpression contains [{"RegularExp":"text","ExpType":"abc"}]
<input type="text" onblur="Reg(Reg)" />

 function Reg(val) {
        alert('Reg');
        alert(val);it display[object object]  i want to get the values


    }

Working Example here 这里的工作示例

Reg is a var and also a function so Reg(Reg) will pass Reg function as a parameter. Reg是一个变量,也是一个函数,因此Reg(Reg)将把Reg函数作为参数传递。

So first change the var name. 因此,首先更改var名称。

Second the JSON.parse(RegularExpression) retrun an object so you cant just get the values by printing the var you need to reach them like here: 其次,JSON.parse(RegularExpression)重新运行一个对象,因此您不能像下面这样通过打印需要获取的var来获取值:

var RegularExpression = " [{\"RegularExp\":\"text\",\"ExpType\":\"abc\"}]";
var regObject = JSON.parse(RegularExpression);

function Reg(val) {
  alert(val[0].RegularExp + '\n' + val[0].ExpType);
}

and the input element should be: 输入元素应为:

<input type="text" onblur="Reg(regObject)" />

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

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