如何从select(selected)jquery中的下拉事件中获取值onchange事件



我在select jquery 的帮助下制作了下拉列表

echo $this->Form->input('Category', 
            array(
                'data-rel' =>'chosen',
                'style' => 'width:220px; margin-bottom:10px',
                'placeholder' => 'Select Category',
                'empty'=>'Select',
                'id'=>'ProductCategoryId',
                array('class'=>'required') 
                )); 

现在我正在尝试检索change的值我试着按照代码来获取值和文本。(我的基本目的是获得价值)

<script>
$(document).ready(function () {
    alert($("#ProductCategoryId option:selected").text()); 
    alert($('#ProductCategoryId').val());
</script>

也许SO上已经有很多关于这个问题的答案了,但这些并没有解决我的问题。

结尾缺少});,您确实需要侦听change事件。

试试这个:

$(document).ready(function () {
    $('#ProductCategoryId').on('change', function() {
        alert( $('option:selected', this).text() ); 
        alert( $(this).val() );
    });
});

最新更新