如何在Mobiscroll tap事件中获取滚动条的值

时间:2013-06-26 02:15:53

标签: javascript html jquery-mobile mobiscroll

跳过箍,在点击事件中获取所选项目。这是我在滚动条中获取所选项目的值所做的。


$('#questions').mobiscroll().select({
    theme: 'wp',
    accent: 'none',
    display: 'modal',
    tap : true, 
    // mode: 'mixed',
    inputClass: 'i-txt',
    setText : 'Select',
    onSelect : function(valueText, inst){ selectedQuestion(valueText, inst); },
    onValueTap : function(item, inst){ tappedQuestion(item, inst); },
    width: 400
});

    function selectedQuestion(valueText, inst){
    console.log( $('#questions').val());
}

function tappedQuestion(obj, inst){
    console.log( "Old Value: "+$('#questions').val() );
    console.log( $(obj).val() ); // this is NULL
    var x = $(obj).get(0).dataset.val;  // have to dig down to get the val of the item
    console.log( "Tapped val: "+x );  // now i got the value
    $('#questions').val(x); // set the Select option to selected
    var tt = $(obj).get(0).innerText;  // now getting the text of the selected option
    $('.ui-btn-text > span.f-dd').text(tt);  // now setting the display text
    $('#questions').mobiscroll('hide'); // hiding the scroller
    console.log( "New Value of Select: "+$('#questions').val() ); // checking we are good
}

这是html。

<select name="questions" id="questions" data-theme="a" class="f-dd"  >
<option value="0">Opt 0</option>
<option value="1">Opt 1</option>
<option  value="2">Opt 2</option>
<option value="3">Opt 3</option>
<option value="4">Opt 4</option>
<option value="5">Opt 5</option>
</select>

也许有更好的方法可以做到这一点?

1 个答案:

答案 0 :(得分:0)

改为使用instance对象。