用jquery标记模态的复选框

我使用Bootstrap模态和jQuery。我正在将字段拖到表单构建器中,它将在下面的表格上打开模态。我想检查id =“编辑地址”,如模态与jQuery打开。我尝试了一种不同的方式,如$(’#编辑地址’)。单击();$(’#编辑地址’)。prop(’checked’,true);等等,但这不起作用。可能是我无法找到正确的选择器。

<div class="modal-dialog modal-lg">
    <div class="modal-content">

        <!-- Modal Header -->
        <div class="modal-header text-center">
            <button type="button" class="close" data-dismiss="modal">×</button>
            <h3 class="semibold modal-title text-primary" style="text-transform: capitalize;">Heading</h3>
        </div>
        <!-- End Modal Header -->

        <div class="modal-body pb0">
            <div class="form-group formtemplate mb0">
                <div class="row">
                    <div class="col-lg-12">
                        <div class="panel form-horizontal form-bordered form-canvas" name="form-profile">
                            <div class="panel-body pt0 pb0">

                                <div class="form-group no-border">

                                    <div class="row">
                                        <div class="col-sm-6">
                                            <label for="edit-address-label" class="control-label mb5">Name:</label>
                                            <input id="edit-address-label" type="text" class="form-control fieldNames" data-option="label" placeholder="The question and name for this field..">
                                        </div>
                                        <div class="col-sm-6 mt20 pt10">
                                            <div class="checkbox custom-checkbox">
                                                <input id="edit-address-required" type="checkbox" data-option="required" value="1">
                                            </div>
                                        </div>
                                    </div>

                                    <div class="row mt10">

                                        <div class="row">
                                            <div class="col-sm-1 mt20 pt10">
                                                <div class="checkbox custom-checkbox">
                                                    <input class="check-address-checkbox" id="edit-address" type="checkbox" data-option="edit-address" value="1">
                                                    <label for="edit-address" checked="">Mark it</label>
                                                </div>
                                            </div>
                                        </div>

                                    </div>
                                </div>
                            </div>
$('#编辑地址')。prop(':checked',true)DiaVolic
this should work but this is not working, maybe i am not able to get selectorSAJ STA
在我写的时候,你使用“:”之前“:”之前?Diavolic
我写的是帖子SAJ STA
“:检查”,不仅仅是“检查”Diavolic

回答 2

  1. 赞同 0

    .click()方法应该在您的情况下工作,检查控制台以查看您是否有任何jQuery问题

    这是一个非常简单的例子,你可以尝试:

    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Checkbox</title>
    </head>
    
    <body>
        <button id="btn">Tick</button>
        <div>
            <input type="checkbox" id="check"> Check
        </div>
        <script src="https://code.jquery.com/jquery-3.6.0.slim.min.js""></script>
            <script>
                $('#btn').click(function () {
                    $('#check').click();
                    console.log(" Checked");
                });</script>
        </body>
    </html>
    

    yessine
  2. 赞同 0

     -try this
     $('#edit-address').attr('checked', true);
    

    Leebogssy
    欢迎堆积溢出,并感谢您贡献答案。你会觉得编辑你的答案,包括解释你的代码吗?这将有助于未来的读者更好地了解正在发生的事情,特别是那些对语言新的社区成员以及努力理解概念的社区成员。Jeremy Caney