How to enable and disable selectmenu JQuery mobile

非 Y 不嫁゛ 提交于 2021-01-27 20:18:39

问题


I have a form that use jquery mobile to generate. I have a dropdown list that initially set to be disabled.

<div data-role="fieldcontain">
    <label for="role-edit" class="select">Project Role:</label>
    <select name="role-edit" id="role-edit" data-native-menu="false" disabled="disabled" class="edit-projectinput">     
        <option value="Admin">Admin</option>
        <option value="Project Manager">Project Manager</option>
        <option value="User">User</option>
    </select>
</div>

I would like to enable the disabled selectmenu using jquery. I tried

$(".edit-projectinput").selectmenu("enable");

But it doesn't work for me.

Could you please instruct me how to enable the disabled selectmenu, and if possible, show me how to disable one.

This is the demo: http://jsfiddle.net/lightbringer/dpv2h/1/


回答1:


Just do :

$(document).ready(function(){
     $("select.edit-projectinput").selectmenu("enable");
});

Demo

Remeber than there will be 2 items with the class .edit-projectinput one the real select that is converted to select menu widget and then the one default selected span element in the widget, so just specifically select the one that matters. Your menu is already initialized just a matter of calling enable method on it.




回答2:


You have to intialize selectmenu first,

$(".edit-projectinput").selectmenu().selectmenu("enable");

and also use unique class name for the select options.




回答3:


I know this is an older post, but came across the same issue in my code and found the issue, so posting here for others. Everything I saw online said to use:

$("selectId").selectmenu("disable");

It didn't work. No error, just not disabling the menu. The fix was a simple # before the ID:

$("#selectId").selectmenu("disable");

Now it disables, no issue :)



来源:https://stackoverflow.com/questions/19330101/how-to-enable-and-disable-selectmenu-jquery-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!