Javascript jQuery:set';选定的';如果值位于数组中,则标记选项
我正在尝试使用jQuery(在Wordpress站点上)将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
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=代码>。