jQuery Mobile多选单击事件

时间:2012-09-04 21:42:55

标签: jquery mobile jquery-mobile multi-select

如何在jQuery Mobile多选上触发click事件?

我正在尝试为选择选项添加关闭按钮,因为我觉得(X)关闭按钮是直接的。这里讨论了这个主题(http://stackoverflow.com/questions/10004719/jquery-mobile-multi-select-box),答案是:

  

您可以添加“关闭”作为选项,然后在单击时触发对话框关闭方法

未讨论如何触发click事件。我尝试过以下方法:

$("#selectmenu").change(function() {
alert('Changed!!');});

$(document).delegate('.ui-selectmenu-screen', 'click', function () {
alert('changed');
}).delegate('.ui-selectmenu .ui-btn-inner', 'click', function (event) {
alert('changed2');});

$('#selectmenu').on('change', function () {
alert('changed');});

$('#selectmenu').live('change', function () {
alert('changed');});

$('#pageName').on('click', '.ui-selectmenu-list > li', function () {
alert('changed');});

$("#selectmenu").live( "click", function(event, ui) {
alert('changed');});

$("#selectmenu").bind( "click", function(event, ui) {
alert('changed');});

1 个答案:

答案 0 :(得分:0)

您将使用$ .selectmenu()方法found here并绑定到change事件,如下所示:

<强> HTML

<div data-role="fieldcontain">
    <select name="selectmenu" id="selectmenu" multiple="multiple">
        <option value="value1">Value 1</option>
        <option value="value2">Value 2</option>
        <option value="value3">Value 3</option>
        <option value="close">Close</option>
    </select>
</div>

关闭选择菜单

$(document).delegate('#selectmenu', 'change', function(){
    var str = $('#selectmenu option:selected').val(); // Get Value of Option

    if (str == 'close'){
        $('#selectmenu').selectmenu('close');
    }
});

添加值

function addValues($el){
    var total = 0;
    $('option', $el).each(function(){
        var value = parseInt($(this).val());
            value = (isNaN(value-0)) ? 0 : parseInt(value); // Defaults Value to 0 if NaN

        total += value;
    });
    return total;
}

JSFiddle for Add Values

我希望这有帮助!