Javascript 如何以数组中的第二项为目标
我正在使用jquery.ui自动完成脚本 请参考这个 因此,与其Javascript 如何以数组中的第二项为目标,javascript,php,jquery,arrays,jquery-ui-autocomplete,Javascript,Php,Jquery,Arrays,Jquery Ui Autocomplete,我正在使用jquery.ui自动完成脚本 请参考这个 因此,与其 colors = [['White', '#fff'], ['Black', '#000'], ['Red', '#f00'], ['Green', '#0f0'], ['Blue', '#00f']]; 我从查询数据库后的PHP脚本中获取相同数组形式的数据。 所以在我的脚本中,colors=data 问题是我需要更改数组显示中的第二项,只需要在值周围添加括号。请问我该怎么做 这是我的剧本 $("document").ready
colors = [['White', '#fff'], ['Black', '#000'], ['Red', '#f00'], ['Green', '#0f0'], ['Blue', '#00f']];
我从查询数据库后的PHP脚本中获取相同数组形式的数据。
所以在我的脚本中,colors=data代码>
问题是我需要更改数组显示中的第二项,只需要在值周围添加括号。请问我该怎么做
这是我的剧本
$("document").ready(function(){
var data = {
"action": "test"
};
data = $(this).serialize() + "&" + $.param(data);
$.ajax({
type: "GET",
dataType: "json",
url: "store2.php?target="+target, //Relative or absolute path to response.php file
data: data,
success: function(data) {
//console.log(data);
var useThis=[];
if(target=="subject")
{
useThis = [{
name: 'level',
minWidth: 'auto',
symbol: ''},
{
name: 'subject',
minWidth: 'auto',
symbol: ' > '},
{
name: 'posts',
minWidth: 'auto',
symbol: ' '},
];
}
else if(target=="location")
{
useThis = [{
name: 'level',
minWidth: 'auto',
symbol: ''}];
}
else
{
useThis = [{
name: 'level',
minWidth: 'auto',
symbol: ''}];
}
var columns = useThis;
colors = data;
//alert(selectThis);
var valueV=targetItem.attr("value");
targetItem.mcautocomplete({
showHeader: true,
columns: columns,
source: colors,
select: function(event, ui)
{
// Set the input box's value
var selectThis = $(this).attr("class").split(" ")[0];//get only the first class
if(target=="subject")
{
this.value = (ui.item ? ui.item[0] +" > " + ui.item[selectThis] : '');
}
else
{
this.value = (ui.item ? ui.item[selectThis] : '');
}
// Set the output div's value
// $('#show_subject') && $('#show_place').text(ui.item ? (target+ ' = ' + ui.item[selectThis] + ui.item[2]) : 'Select a subject');
if(target=="subject")
{
$('input[name="try"]').val(ui.item[3]);
}
if(target=="location")
{
$('input[name="lat"]').val(ui.item[1]);
$('input[name="lon"]').val(ui.item[2]);
}
if(target=="tutor")
{
$('input[name="tutor"]').val(ui.item[0]);
}
return false;
}
});
}
});
PHP
//“catname”、“subname”、“subcount”和“catid”是数据库中的列名。所以我不能在这里加括号,否则我就不能得到正确的值
$sql=$statement1;
$v_1="catname";
$v_2="subname";
$v_3="subcount";
$v_4="catid";
$stmt =connection::$pdo->prepare($sql);
$stmt->execute();
$json=array();
while($row = $stmt->fetch())
{
array_push($json,array($row[$v_1],$row[$v_2],$row[$v_3],$row[$v_4]));
}
echo json_encode($json)
那么,如何针对特定项目并在其周围添加括号?我在数组中的第二个项目上添加了括号:
source: jQuery.each(colors , function(index, value){
console.log(value[1]);
value[2] = "("+value[1]+")";
}),
更改\u renderItem
函数中的每个循环,如下所示。希望这对你有帮助
$.each(this.options.columns, function(index, column) {
var i = column.valueField ? column.valueField : index;
var itm=item[i];
if (i==1)
itm='('+item[i]+')';
t += '<span style="float:left;min-width:' + column.minWidth + ';">' + itm+'</span>'
});
$.each(this.options.columns,函数(索引,列){
var i=column.valueField?column.valueField:索引;
var itm=项目[i];
如果(i==1)
itm='('+项目[i]+')';
t+=''+itm+''
});
请检查更新的。我已经添加了他们的列索引
名称。你可以这样做。@ParthTrivedi,我知道这一点,这就是我如何将我的第三个forths值添加到数组中的。但是当第二项不是字符串值而是整数时,如何在其周围添加括号?请检查使用数组,如[['White'、'fff'、'(0)'、'Black'、'000'、'(1)'、'Red'、'f00'、'(3)'、'Green'、'0f0'、'(4)、'Blue'、'00f'、'(5)]
在sql中添加另一列,并使其成为子countString
,并在其上添加括号。
$.each(this.options.columns, function(index, column) {
var i = column.valueField ? column.valueField : index;
var itm=item[i];
if (i==1)
itm='('+item[i]+')';
t += '<span style="float:left;min-width:' + column.minWidth + ';">' + itm+'</span>'
});