Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery:set';选定的';如果值位于数组中,则标记选项_Javascript_Jquery_Selectedindex - Fatal编程技术网

Javascript jQuery:set';选定的';如果值位于数组中,则标记选项

Javascript jQuery:set';选定的';如果值位于数组中,则标记选项,javascript,jquery,selectedindex,Javascript,Jquery,Selectedindex,我正在尝试使用jQuery(在Wordpress站点上)将selected属性添加到选项标记中。应选择的值存储在PHP数组中。为什么选定的属性未添加到。 我的脚本.js jQuery("#location option").each(function($){ var arr = [<?php echo '"'.implode('","', $php_array).'"' ?>]; // for testing also hardcoded array: doesn't work e

我正在尝试使用jQuery(在Wordpress站点上)将
selected
属性添加到
选项
标记中。应选择的值存储在PHP数组中。为什么选定的
属性未添加到

我的脚本.js

jQuery("#location option").each(function($){

var arr = [<?php echo '"'.implode('","', $php_array).'"' ?>];
// for testing also hardcoded array: doesn't work either
// var arr = ["1", "2"];

   if(jQuery.inArray($(this).val(),arr) != -1){
     $(this).attr('selected', 'selected');
   };

});

问题是您无法将任何内容传递给
每个
处理程序。从中删除
$
,代码即可正常工作:

var arr = ["1", "2"];
jQuery("#location option").each(function () {
    if (jQuery.inArray($(this).val(), arr) != -1) {
        $(this).prop('selected', true);
    };
});

问题在于您无法将任何内容传递给
每个
处理程序。从中删除
$
,代码即可正常工作:

var arr = ["1", "2"];
jQuery("#location option").each(function () {
    if (jQuery.inArray($(this).val(), arr) != -1) {
        $(this).prop('selected', true);
    };
});

我认为jQuery循环中有输入错误

jQuery("#location option").each(function($){

var arr = [<?php echo '"'.implode('","', $php_array).'"' ?>];
// for testing also hardcoded array: doesn't work either
// var arr = ["1", "2"];

   if(jQuery.inArray($(this).val(),arr) != -1){
     $(this).attr('selected', 'selected');
   };

});
jQuery(“#位置选项”)。每个(函数($){
var-arr=[];
//用于测试硬编码阵列:也不起作用
//var arr=[“1”,“2”];
if(jQuery.inArray($(this.val(),arr)!=-1){
$(this.attr('selected','selected');
};
});
函数($)应该没有参数,就像这个函数()

工作代码:

jQuery("#location option").each(function(){

var arr = [<?php echo '"'.implode('","', $php_array).'"' ?>];
// for testing also hardcoded array: doesn't work either
// var arr = ["1", "2"];

   if(jQuery.inArray($(this).val(),arr) != -1){
     $(this).attr('selected', 'selected');
   };

});
jQuery(“#位置选项”)。每个(函数(){
var-arr=[];
//用于测试硬编码阵列:也不起作用
//var arr=[“1”,“2”];
if(jQuery.inArray($(this.val(),arr)!=-1){
$(this.attr('selected','selected');
};
});

我的JSFIDLE:

我认为您在jQuery循环中有输入错误

jQuery("#location option").each(function($){

var arr = [<?php echo '"'.implode('","', $php_array).'"' ?>];
// for testing also hardcoded array: doesn't work either
// var arr = ["1", "2"];

   if(jQuery.inArray($(this).val(),arr) != -1){
     $(this).attr('selected', 'selected');
   };

});
jQuery(“#位置选项”)。每个(函数($){
var-arr=[];
//用于测试硬编码阵列:也不起作用
//var arr=[“1”,“2”];
if(jQuery.inArray($(this.val(),arr)!=-1){
$(this.attr('selected','selected');
};
});
函数($)应该没有参数,就像这个函数()

工作代码:

jQuery("#location option").each(function(){

var arr = [<?php echo '"'.implode('","', $php_array).'"' ?>];
// for testing also hardcoded array: doesn't work either
// var arr = ["1", "2"];

   if(jQuery.inArray($(this).val(),arr) != -1){
     $(this).attr('selected', 'selected');
   };

});
jQuery(“#位置选项”)。每个(函数(){
var-arr=[];
//用于测试硬编码阵列:也不起作用
//var arr=[“1”,“2”];
if(jQuery.inArray($(this.val(),arr)!=-1){
$(this.attr('selected','selected');
};
});

我的JSFIDLE:

提示:使用
json_encode
将PHP数组注入JavaScript源代码:
var arr=
.Tip:使用
json_encode
将PHP数组注入JavaScript源代码:
var arr=