[英]remove quotes from json_encode keys
I am new to jquery and I am trying to use Jquery UI to create an auto-complete.I'm still learning so I'm sure my code could be a lot better but this is just a start. 我是jquery的新手,我正在尝试使用jquery UI创建自动完成功能。我仍在学习,因此我确定我的代码可能会好很多,但这只是一个开始。 I am having a problem where jquery is putting quotes around the json keys.I've had a look though stackoverflow and I can't seem to find a solution so I thought it's worth asking as I am well and truly stuck.I think it must be something wrong with my php somewhere. 我遇到一个问题,即jquery在json键周围加上引号。我虽然看了stackoverflow,但似乎找不到解决方案,所以我认为值得一问,因为我身体健康且确实卡住了。一定是我的PHP在某处出了问题。
{"value":"Managerial Accountants","id":"5929"}
I want my output to come out like this" 我希望我的输出像这样“
{value:"Managerial Accountants",id:"5929"}
This is the rest of my code: 这是我其余的代码:
<script>
jQuery(function(){
jQuery(function () {
var checkboxval;
var availableTags = [
<?php
// Database Connection
error_reporting(-1);
ini_set('display_errors', 'On');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$q="SELECT name as value,term_id as id FROM 7terms WHERE term_id not in
(select parent from 7term_taxonomy) and term_id in (select term_id from
7term_taxonomy where taxonomy='cat')";
$r = mysqli_query($con, $q);
$city_state = array();
while($row = mysqli_fetch_assoc($r)){
$rows[]=$row;
}
$json = json_encode($rows,true);
echo $json;
?>
];
//set autocomplete search
set_autocomplete_search("tags");
set_autocomplete_search("tags1");
set_autocomplete_search("tags2");
function set_autocomplete_search(p_tags) {
var temp_p_tags = "#" + p_tags;
jQuery(temp_p_tags).autocomplete({
source: availableTags,
select: function (event, ui) {
var txtbx1 = (ui.item.name);
var catid = (ui.item.id);
alert(catid);
jQuery(temp_p_tags).val(txtbx1);
var tags = jQuery(temp_p_tags).val(txtbx1);
//var checkboxval = "";
checkboxval = tags.val();
jQuery("#" + checkboxval + "").prop("checked", true);
},
change: function () {
//alert("changed detected");
//$("#" + checkboxval + "").prop("checked", false);
}
}).blur(function(event) {
event.stopPropagation();
event.preventDefault();
//uncheck all checkboxes first
jQuery(".test input:checked").each(function() {
jQuery(this).attr("checked", false);
});
//set checkbox
var checkbox_tags = jQuery("#tags").val();
jQuery("#" + checkbox_tags + "").prop("checked", true);
var checkbox_tags1 = jQuery("#tags1").val();
jQuery("#" + checkbox_tags1 + "").prop("checked", true);
var checkbox_tags2 = jQuery("#tags2").val();
jQuery("#" + checkbox_tags2).prop("checked", true);
});
}
});
});//]]>
Thanks for your time.I really appreciate any help I can get with this as I still have a lot to learn and have been stuck on this for a couple of days. 感谢您的宝贵时间。我真的很感谢我能为此提供的任何帮助,因为我还有很多东西要学习,而且已经坚持了几天。
You are experiencing this issue because you are not using json correctly. 您正在遇到此问题,因为您没有正确使用json。
You can still just echo the json_encode()
into the JS variable but get rid of the []
brackets and then use .parseJSON()
to get what you need: 您仍然可以只将json_encode()
回显到JS变量中,但是去掉[]
括号,然后使用.parseJSON()
获得所需的内容:
var availableTags = $.parseJSON('<?php
// Database Connection
error_reporting(-1);
ini_set('display_errors', 'On');
$con=mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$q="SELECT name as value,term_id as id FROM 7terms WHERE term_id not in
(select parent from 7term_taxonomy) and term_id in (select term_id from
7term_taxonomy where taxonomy='cat')";
$r = mysqli_query($con, $q);
$city_state = array();
while($row = mysqli_fetch_assoc($r))
{
$rows[]=$row;
}
echo json_encode($rows,true);
?>');
This solution should get you going but I would recommend opting for an AJAX based solution where your query is in a PHP file and call upon it with AJAX because $.ajax() can auto-convert a JSON string into the properly formatted array/object expected as long as you set dataType: 'json'
该解决方案可以助您一臂之力,但我建议选择一种基于AJAX的解决方案,其中您的查询位于PHP文件中,并使用AJAX对其进行调用,因为$ .ajax()可以将JSON字符串自动转换为格式正确的数组/对象只要您设置dataType: 'json'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.