【发布时间】:2016-11-23 22:08:16
【问题描述】:
我有一个脚本可以从数据库中填充数据,但我尝试使用的变量 selected 似乎没有被使用。我的意思是 Netbeans 告诉我该变量未使用。脚本有问题吗?
function get_child_options(selected)
{
if (typeof selected === 'undefined')
{
var selected = ' ';
}
var parentID = jQuery('#parent').val();
jQuery.ajax(
{
url: '/MyProjectName/admin/parsers/child_categories.php',
type: 'POST',
data:
{
parentID: parentID,
selected: selected
},
success: function(data)
{
jQuery('#child').html(data);
},
error: function()
{
alert("Something went wrong with the child options.")
},
});
}
jQuery('select[name="parent"]').change(get_child_options);
【问题讨论】:
-
var selected在块范围之外。将var selected移动到if(typeof之前 -
selected 已在函数参数中定义。在@dxcorzo 在他的回答中显示之前删除
var。