Jquery选择更改

 Afghanistan  Albania  Algeria  Andorra .... etc  

而这个js

 $(document).ready(function() { $('#Nazione').change(function(){ alert( $(this).find("option:selected").attr('prefix') ); alert( $(this).attr('prefix') ); }); }); 

我有警告NULL ……为什么?

你的代码很好。 这是一个演示: http : //jsfiddle.net/hKktc/1/

您没有获得第二个警报调用值的原因是因为select的属性prefix不存在而且仅对option存在

在您的代码中, $(this)引用 ,而不是选项。 上不存在prefix属性

这将导致第二个示例的问题。

第二个alert将返回null,因为没有属性prefix

你究竟期待什么?

我发现第二个警报 – alert( $(this).attr('prefix') ); 是导致问题的那个。 您可以获得前缀编号的警报,然后获得null警报(由第二个警报引起)。

你可能想要.val()而不是.attr('prefix')来获得选定值。

 $(document).ready(function() { $('#Nazione').change(function(){ alert( $(this).find("option:selected").attr('prefix') ); alert( $(this).val('prefix') ); }); });