简体   繁体   English

jQuery对话框中的复选框触发更改事件,但没有任何反应

[英]Checkbox within jQuery dialog firing change event but nothing happens

I use a div (wrapper) to generate a form on the fly : 我使用div(包装器)动态生成表单:

<div id="wrapper"></div>

Then I fill it up in the javascript and turn it into a dialog that display some information and a checkbox list 然后,将其填充到javascript中,然后将其变成一个显示一些信息和复选框列表的对话框

<div id="formulaire" title="Insérer un nouvel utilisateur">
    <p>Remplir le fomrulaire suivant :</p>
    <label for="user" style="display:inline-block;width:130px;">Utilisateur (WF) : </label><span id="nom"></span><br />
    <label for="mail" style="display:inline-block;width:130px;">Adresse E-mail : </label><span id="prenom"></span><br /><br />
    <label for="permission">L'utilisateur a les permissions suivantes : </label><br />

<ol>
    <li><input type="checkbox" id="Checkbox1" /> Gérer la fréquence des visites</li>
    <li><input type="checkbox" id="Checkbox2" /> Chercher un client sur le serveur</li>
    <ul><li id="Li1"><input type="checkbox" id="Checkbox3" />Télécharger ce client</li></ul>
    <li><input type="checkbox" id="Checkbox4" /> Proposer un prix inférieur au prix minium</li>
    <li><input type="checkbox" id="Checkbox5" /> Créer des &quot;Price Credit Note&quot;</li>
    <li><input type="checkbox" id="Checkbox6" /> Créer des &quot;Quote Approval&quot;</li>
    <li><input type="checkbox" id="Checkbox7" /> Créer des &quot;Price Afreement&quot;</li>
</ol>

    <input type="button" value="Valider"/>
</div>

with the following javascript : 使用以下javascript:

function CreateUser(nom, prenom){
    $("#wrapper").empty();
    $("#wrapper").append()//Here I append the html above

    $("#nom").text(nom);
    $("#prenom").text(prenom);
    $("#Li1").hide();

    $("#Checkbox2").change(function () {
        if (this.checked) {
            $("#Li1").show()
        }
        else {
            $("#Li1").hide()
        }
    });

    $("#formulaire").dialog({
        modal: true,
        width: 450
    });
}

This function is called by a button, The dialog shows up properly but I want the 3rd element (which is a sub list) on the list to appear only if Checkbox2 is checked. 此功能由一个按钮调用,该对话框会正确显示,但我希望仅在选中Checkbox2的情况下,列表中的第3个元素(这是子列表)才会出现。 The issue is that even though the change event fires, if I close the dialog and open a new one, #Li1 no longer shows or hide, nothing happens when I check/uncheck Checkbox2. 问题是,即使触发了更改事件,如果我关闭对话框并打开一个新对话框,#Li1也不再显示或隐藏,当我选中/取消选中Checkbox2时什么也不会发生。 Where does the issue come from ? 问题是从哪里来的?

this keyword represent the current object. 关键字表示当前对象。 as you are using jQuery callback it is better to use it in following manner... 当您使用jQuery回调时,最好以以下方式使用它...

$("#Checkbox2").change(function () {
            if ($(this).attr("checked")) {
                $("#Li1").show()
            }
            else {
                $("#Li1").hide()
            }
        });

You can also check for checkbox in following manner 您也可以按照以下方式检查复选框

$(this).is(':checked')

Thanks for the details of step you followed to reproduce this problem. 感谢您遵循步骤以重现此问题的详细信息。 This is better to destroy the dialog so jQuery take proper care of underlying elements as well as associated events. 最好销毁对话框,以便jQuery适当照顾基础元素以及相关事件。 Hence destroy the dialog properly and use $(this) to access source element in event. 因此,请正确销毁对话框,并在事件中使用$(this)访问源元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM