我正在使用 Yii2-advanced-app。我想将我选择的下拉列表的值发送到同一页面上的 php 代码(显示带有复选框的列表)。请参阅下图以供引用(这就是我实际想要的)-
但是,从下拉列表中选择一些元素后,我得到了结果,但整个 html 页面都在该特定部分中。这是供引用的快照 -
我不明白为什么我会收到整个页面的响应。
我为此编写了以下代码 -
下拉列表
<?= $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。
我收到这样的响应
最佳答案
您可以在 Controller 操作上使用 renderPartial
来进行 Ajax 调用。
查看详情here
关于javascript - yii2 : Sending data to same page not working properly,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36477096/