javascript - yii2 : Sending data to same page not working properly

标签 javascript php jquery ajax yii2

我正在使用 Yii2-advanced-app。我想将我选择的下拉列表的值发送到同一页面上的 php 代码(显示带有复选框的列表)。请参阅下图以供引用(这就是我实际想要的)-

Please see this image for reference

但是,从下拉列表中选择一些元素后,我得到了结果,但整个 html 页面都在该特定部分中。这是供引用的快照 -

Here is the Snap for reference

我不明白为什么我会收到整个页面的响应。

我为此编写了以下代码 -

下拉列表

<?= $form->field($countries[0], 'id')->dropDownList(
    ArrayHelper::map($countries, 'phonecode', 'name'),
    [
    'prompt' => 'Select Country',
    'onchange' => '
    var id;
    jQuery.ajax({
        type: "POST",
        data: {
            "id": $(this).val(), 
        },
        // url: "",
        success: function(data){
            jQuery(".tobechanged").html(data);
            alert(data);
        }
    });'
    ])->label(false);
    ?>

我各自的 div 是

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged" style="max-height: 400px; background: white; overflow: auto;">
    <ul class="list-unstyled" id="chkall">
        <?php
        if(isset($contacts)) {
            $contacts = $contacts;
                            // print_r($contacts[0]['cust_country_code']);exit();
            foreach ($contacts as $contact) {
                if(isset($_POST['id'])) {
                    if($contact['cust_country_code'] == $_POST['id']) {
                        echo '<li>
                        <div class="checkbox" id="checkboxes">
                            <label><input type="checkbox" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label>
                        </div>
                    </li>';
                }
            } else {
                echo '<li>
                <div class="checkbox" id="checkboxes">
                    <label><input type="checkbox" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label>
                </div>
            </li>';
        }
    }
}
?>
</ul>
</div>

即使我们显示“Hello”而不是复选框代码,它也不起作用。即像这样-

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged" style="max-height: 400px; background: white; overflow: auto;">
    Hello
</div>

所以,请帮助我避免不需要的 html。

我收到这样的响应

Response

最佳答案

您可以在 Controller 操作上使用 renderPartial 来进行 Ajax 调用。

查看详情here

关于javascript - yii2 : Sending data to same page not working properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36477096/

相关文章:

javascript - 使用 HTML5 验证多个电子邮件地址

javascript - firefox ajax调用返回NS_ERROR_DOM_BAD_URI或jsonp false错误

php - 使用 AJAX 将复选框值(如果未选中则为 0)传递给 PHP

javascript - 全宽 slider - 交叉淡入淡出过渡问题

javascript - 在 DataTable 中使用 LINQ to list<AJAX>

javascript - 替换图像时缩放图像以适合

javascript - 馅饼 block 太小

javascript - 无法检索 json 数据

php - 如何构建支持多语言(英语、法语、中文等)的 PHP 应用程序

php - Ajax调用php函数