如何使用JQuery-ui将初始化为禁用的复选框/单选按钮启用

huangapple go评论56阅读模式
英文:

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 -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://code.jquery.com/ui/1.13.2/jquery-ui.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function(){
    $(&quot;#openchest&quot;).checkboxradio();
    $(&quot;#enablebutton&quot;).click(function(){
        $(&quot;#openchest&quot;).prop(&quot;disabled&quot;, false); // Doesnt work
        $(&quot;#openchest&quot;).checkboxradio(&quot;enable&quot;); // Also doesn&#39;t work
    });
});
&lt;/script&gt;
&lt;button id=&quot;enablebutton&quot;&gt;enable checkbox&lt;/button&gt;
&lt;label for=&quot;openchest&quot;&gt;Open Chest&lt;/label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;openchest&quot; disabled=&quot;disabled&quot; value=&quot;1&quot; /&gt;

<!-- 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&#39;s checked or disabled state is changed programmatically.

Here is the correct code that works:

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://code.jquery.com/ui/1.13.2/jquery-ui.js&quot;&gt;&lt;/script&gt;
&lt;script&gt;
$(document).ready(function(){
    $(&quot;#openchest&quot;).checkboxradio();
    $(&quot;#enablebutton&quot;).click(function(){
        $(&quot;#openchest&quot;).prop(&quot;disabled&quot;, false);
        $(&quot;#openchest&quot;).checkboxradio(&quot;refresh&quot;);
    });
});
&lt;/script&gt;
&lt;button id=&quot;enablebutton&quot;&gt;enable checkbox&lt;/button&gt;
&lt;label for=&quot;openchest&quot;&gt;Open Chest&lt;/label&gt;&lt;input type=&quot;checkbox&quot; id=&quot;openchest&quot; disabled=&quot;disabled&quot; value=&quot;1&quot; /&gt;

huangapple
  • 本文由 发表于 2023年4月20日 03:16:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/76058096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定