jQuery mobile:动态填充DropDown-List

时间:2014-02-23 11:44:52

标签: javascript jquery html jquery-mobile

我正在使用jQuery mobile动态填充下拉列表。但问题是:开始时没有预先选择的条目,它只是emtpy。用户必须点击下拉列表并选择第一个条目以将其选中。但应该预先选择 - 这有什么办法/问题吗?继承人下载:

            <select name="selectQuestion" id="selectQuestionReset">

这里填充它的代码:

$.post('url, { }, function(data){
    data = $.trim(data);
    $('#selectQuestionReg').empty();

    if(data.length>1){
        data = JSON.parse(data);
        data.forEach(function(i) {
            $('<option value="' + i.frage + '" id="' + i.id + '">' + i.frage + '</option>').appendTo($('#selectQuestionReg'));
            $('#selectQuestionReg').trigger("chosen:updated");
        })
    }else{
        jError(
            'Fehler beim Anfordern der Sicherheitsfragen!',
            {
                autoHide : true, 
                TimeShown : 2000,
                HorizontalPosition : 'center',
                VerticalPosition : 'top'
            }); 
    }

它的样子:http://s14.directupload.net/images/140223/6wu87wge.jpg 应该有第一个选项已经显示为选中..

1 个答案:

答案 0 :(得分:0)

您可以在回复之前追加一个选项标签。 1)<option value="0">Select option<option>或 2)<option value="0"><option>

当您提交时间表格时,下拉列表的值为“0”。喜欢 if($('#selectQuestionReg').val()==0){ alert("Please select option"); } `$ .post('url,{},function(data){     data = $ .trim(data);     $( '#selectQuestionReg')空();

if(data.length>1){
    data = JSON.parse(data);

$('#selectQuestionReg')。html(“选择选项”); //此处删除之前存在的数据并添加新数据         data.forEach(function(i){             $(''+ i.frage +'')。appendTo($('#selectQuestionReg'));             $( '#selectQuestionReg')触发器( “选自:更新”);。         })     }其他{         jError(             'Fehler beim Anfordern der Sicherheitsfragen!',             {                 autoHide:是的,                 TimeShown:2000,                 Horizo​​ntalPosition:'center',                 VerticalPosition:'顶'             });     }`

相关问题