JQuery Mobile Custom Flipbox get selected value

2019-08-10 06:18发布

问题:

I have the following customflipbox created using the Datebox jQM plugin:

<div data-role="fieldcontain">
    <label for="cf" data-i18n="config.localelabel"></label>
    <input name="cf" type="date" data-role="datebox" id="cf" data-options='{"mode":        "customflip",
"customData": [  {
"input": true,
"name": "",
"data": ["Italiano","English","Espanol","Deutsch","Francais","русский"]
}],
"useNewStyle": false,
"overrideStyleClass": "ui-icon-dice",
"useButton":false,
"useFocus" : true
}' />

When I pick a value I need to populate the cf input field with the selected value, I tried:

$('cf').on('datebox',function(p,e){
    if (p.method === 'set') {
        e.stopImmediatePropagation();
        $(this).val(selectdata[p.value]);
    }
});

on the 'datebox' event, but it seems the most i can get is that the field is populated with the index of the selected value but I need the actual string (e.g. English).

Any insight?

Thank you. M.

回答1:

Add the data array in code and then you can reference it in 'set' event:

<div data-role="fieldcontain">
    <label for="cf" data-i18n="config.localelabel"></label>
    <input name="cf" type="date" data-role="datebox" id="cf" data-options='{"mode":        "customflip",
"useNewStyle": false,
"overrideStyleClass": "ui-icon-dice",
"useButton":false,
"useFocus" : true
}' />

In code create a global variable called selectdata that is the array of values:

var selectdata = ["Italiano","English","Espanol","Deutsch","Francais","русский"];
jQuery.extend(jQuery.mobile.datebox.prototype.options, {
    'customData': [{
        'input': true,
            'name': '',
            'data': selectdata
    }],
        "useHeader": false,
        "overrideCustomSet": "OK"
});

$('#cf').on('datebox', function (e, p) {
    if (p.method === 'set') {
        e.stopImmediatePropagation();
        $(this).val(selectdata[p.value]);
    }
});

DEMO