1

我有代码(根据 Brad Christie 的建议进行了编辑):

drupal_add_js('

jQuery(document).ready(function(){ 

jQuery("#selFinishes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_color . ') {
jQuery(this).attr("selected","selected")
}   
})

});
', "inline");

它成功地将“选定”属性(通过 PHP 添加 $filter_color)添加到选定值。但是当我像这样定位多个选择字段时:

   drupal_add_js('

jQuery(document).ready(function(){ 

jQuery("#selFinishes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_color . ') {
jQuery(this).attr("selected","selected")
}   
})

 jQuery("#selThemes option").each(function(index) {
if (jQuery(this).val() == ' . $filter_theme . ') {
jQuery(this).attr("selected","selected")
}   
})

});
', "inline");

两个循环都无法工作!

感谢您的提示!

4

2 回答 2

2

上面的代码显然是混合了 javascript 和 php,但无法判断它可能有什么问题。您应该检查(通过在浏览器中查看源代码)生成的 javascript 代码是否是您想要的。如果可能,请在此处发布生成的 javascript。

于 2012-09-11T15:05:37.150 回答
1

我有根据的猜测是,生成的 JavaScript 在输出时是无效的。通过变量名 ( $filter_color& $filter_theme) 猜测其中一个(或两个)很可能是导致测试if (...val() == string)失败的字符串,这在术语中会使整个 JavaScript 块导致语法错误并完全失败。

尝试更改输出以将值包含在字符串中(可能类似于以下内容):

[snip].val() == "' . $filter_theme . '") {[/snip]

请注意,我在 PHP 连接前后的变量两侧添加了双引号。这应该修复语法应该让实习生再次工作。

同样,您的问题中的信息太少,但这将是我对解决方案的猜测

于 2012-09-11T15:13:36.563 回答