jQuery选择更改 [英] Jquery select change

查看:70
本文介绍了jQuery选择更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<select id="Nazione" name="Nazione">
    <option prefix='+93' value='AF' >Afghanistan </option>
    <option prefix='+355' value='AL' >Albania </option>
    <option prefix='+213' value='DZ' >Algeria </option>
    <option prefix='+376' value='AD' >Andorra .... etc
</select>

和这个js

$(document).ready(function() {  
    $('#Nazione').change(function(){

        alert( $(this).find("option:selected").attr('prefix') );
        alert( $(this).attr('prefix') );
    });
  });

我有警报NULL ... 为什么?

I have alert NULL... WHy?

推荐答案

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

Your code is fine. Here's a demo : http://jsfiddle.net/hKktc/1/

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

The reason you're not getting a value for the second alert call is because the attribute prefix doesn't exist for the select and only exists for the option

这篇关于jQuery选择更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆