将onchange问​​题作为jquery移动选择菜单的一部分?

时间:2014-03-31 09:01:08

标签: javascript jquery html jquery-mobile

我无法将选择的值作为选择菜单的一部分,我的代码如下:

HTML:

<div data-role="main" class="ui-content">
<div data-role="fieldcontain">
    <label for="select-choice-1" class="select">Choose shipping method:</label>
    <select name="select-choice-1" id="select-choice-1">
        <option value="standard">Standard: 7 day</option>
        <option value="rush">Rush: 3 days</option>
        <option value="express">Express: next day</option>
        <option value="overnight">Overnight</option>
    </select>
</div>
</div>

脚本:

<script type="text/javascript">
$('#select-choice-1').on('change', function() {
      alert( this.value );
    })
</script>

如何获取值onChange函数。

2 个答案:

答案 0 :(得分:1)

尝试将代码包装在 DOM ready 处理程序$(document).ready(function() {...});或更短的$(function() {...});内,以确保在执行jQuery代码之前已正确加载DOM元素:

$(function() {
    $('#select-choice-1').on('change', function() {
      alert( this.value );
    })
});

对于jQuery mobile,最好使用 pagecreate 事件:

  

在DOM中创建页面时触发(通过ajax或   其他)并且所有小部件都有机会增强   包含标记。

$(document).on('pagecreate', function() {
    $('#select-choice-1').on('change', function() {
      alert( this.value );
    })
});

答案 1 :(得分:0)

http://jsfiddle.net/QvG2R/1/

使用以下语法

$('#select-choice-1').change(function() {

      alert( this.value );
    });