【问题标题】:How to listen Jquery mobile event?如何监听 Jquery 移动事件?
【发布时间】:2015-09-22 00:35:36
【问题描述】:
<script>
$(document).ready(function() {
     $("#select-native-1 option:selected").select( function () { 
    alert("a");
    });
});
</script>
...
<form>
    <div class="ui-field-contain">
        <select name="select-native-1" id="select-native-1">
            <option id="burnabyCampus" value="1">A</option>
            <option id="downtownCampus" value="2">B</option>
        </select>
    </div>
</form>

我正在尝试从 jQuery Mobile 监听选项选择事件。但是我不知道正确的做法是什么?我在哪里可以找到它?

我现在怎么了?

【问题讨论】:

    标签: jquery events mobile


    【解决方案1】:

    您应该注意输入何时更改,而不是文档准备好时...

    $('#select-native-1').change(function(){
        alert('input changed');
    });
    

    见小提琴——https://jsfiddle.net/6L08nj2u/11/

    编辑:您仍应将 jquery 包装在“就绪”中,但将此函数绑定到输入的“更改”。所以如果这就是你所有的js,

    $(document).ready(function() {    // wrap everything in ready
        $('#select-native-1').change(function(){
            alert('input changed');
        });
    });
    

    所以现在如果你想在 text 值改变之前得到它,让我们把它放在 ready 里面。

    $(document).ready(function() {    // wrap everything in ready
    // page is loaded
    
    // what the value is as soon as page is done loading
    $('#select-native-1 option:selected').text();
    
    
    // watch for every time the value changes
            $('#select-native-1').change(function(){
                alert('input changed');
            });
    
        });
    

    【讨论】:

    • 谢谢。我没有提到的一件事是 jQuery Mobile 基本选择默认选择了第一个选项 - 或者至少看起来如此。您可以在页面加载时收听选择事件吗? (默认选择)
    • 还有 api doc for change()??找不到
    • api.jquery.com/change ,我从来没有说过我们正在观看文本更改 - 在过去的更新中,我向您展示了如何选择文本,因为没有它并尝试通过 .val() 访问它给你 1,2,3 等等,看看代码,我们正在选择你的输入并将其绑定到“更改”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多