我有一个我不能理解的问题。我做了很多测试,但结果就是让我很困扰。我有一个ajax请求,我从那里获取数据,如下所示:
$check = 0;
$output = '';
while($data = mysqli_fetch_assoc($result1)){
if($check == 0){
$output = $data['license'];
$check = 1;
}else{
$output .= ','.$data['license'];
}
}
echo $output;最后的输出是"2020,2021“。现在我在js中有了我的alert来测试结果:
success: function(data) {
alert(data); //get "2020,2021" from this alert
var tab = data.split(',');
alert(tab[0]+','+tab[1]); //get "2020,2021" from this alert
if(tab[0]=='2020'){
alert('yes1');
}else{
alert('no1');
}
if(tab[1]=='2021'){
alert('yes2');
}else{
alert('no2');
}
}但现在的问题是:我收到的另外两个警报是"yes1“和"no2”……我的2020怎么可能等于2020,2021不等于2021。我就是不明白,如果有人能帮上忙。
更新:当我提醒tab1+','+tab时,我有:
“2021年
,2020“
我不知道何时可以追加(我的"SELECT * FROM my_table WHERE license='2021‘“获取所有结果,所以它不在我的数据库中)
发布于 2020-06-19 20:48:33
尝试此操作(在代码中将success =更改为success :
success = function(data) {
console.log(data); //get "2020,2021" from this alert
const [tab0, tab1] = data.split(',');
console.log(tab0 === '2020' ? 'yes0' : 'no0');
console.log(tab1 === '2021' ? 'yes1' : 'no1');
}
// testing
success("2020,2021")
success("2020, 2021")
然后试试这个:
success = function(data) {
console.log(data); //get "2020,2021" from this alert
const [tab0, tab1] = data.match(/\d{4}/g);
console.log(tab0 === '2020' ? 'yes0' : 'no0');
console.log(tab1 === '2021' ? 'yes1' : 'no1');
}
// testing
success("2020,2021")
success("2020, 2021")
发布于 2020-06-19 20:44:24
如您所见,数据是否正确脚本工作:
var data='2020,2021';
var tab = data.split(',');
alert(tab[0]+','+tab[1]); //get "2020,2021" from this alert
if(tab[0]=='2020'){
alert('yes1');
}else{
alert('no1');
}
if(tab[1]=='2021'){
alert('yes2');
}else{
alert('no2');
}
https://stackoverflow.com/questions/62470521
复制相似问题