英文:
How do you enable a checkboxradio with JQuery-ui that was initialized as disabled
问题
You can change the code like this to make the button re-enable the checkbox initialized by jQuery UI:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#openchest").checkboxradio();
$("#enablebutton").click(function(){
$("#openchest").prop("disabled", false).checkboxradio("refresh"); // This should work
});
});
</script>
<button id="enablebutton">enable checkbox</button>
<label for="openchest">Open Chest</label><input type="checkbox" id="openchest" disabled="disabled" value="1" />
This code combines prop("disabled", false)
and checkboxradio("refresh")
to enable the checkbox and refresh its appearance when the button is clicked.
英文:
I have a checkbox that I need to have enabled and disabled when another action happens on the page.
The checkbox is altered using jquery-ui and thus it looks pretty.
Here is the pseudo code:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-html -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#openchest").checkboxradio();
$("#enablebutton").click(function(){
$("#openchest").prop("disabled", false); // Doesnt work
$("#openchest").checkboxradio("enable"); // Also doesn't work
});
});
</script>
<button id="enablebutton">enable checkbox</button>
<label for="openchest">Open Chest</label><input type="checkbox" id="openchest" disabled="disabled" value="1" />
<!-- end snippet -->
What do I need to change to make the button re-enable the checkbox initialized by jquery-ui?
答案1
得分: 0
Refresh()
刷新小部件的可视状态。在原生元素的选中或禁用状态在程序中被更改后,用于更新的功能。
以下是正确的可用代码:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#openchest").checkboxradio();
$("#enablebutton").click(function(){
$("#openchest").prop("disabled", false);
$("#openchest").checkboxradio("refresh");
});
});
</script>
<button id="enablebutton">启用复选框</button>
<label for="openchest">打开宝箱</label><input type="checkbox" id="openchest" disabled="disabled" value="1" />
英文:
After going through the documentation intending to destroy and re-initialize the widget, I discovered that you need to add refresh to the widget.
Refresh()
Refreshes the visual state of the widget. Useful for updating after the native element's checked or disabled state is changed programmatically.
Here is the correct code that works:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.13.2/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$("#openchest").checkboxradio();
$("#enablebutton").click(function(){
$("#openchest").prop("disabled", false);
$("#openchest").checkboxradio("refresh");
});
});
</script>
<button id="enablebutton">enable checkbox</button>
<label for="openchest">Open Chest</label><input type="checkbox" id="openchest" disabled="disabled" value="1" />
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论