我正在使用Yii2-advanced-app。我想將我選擇的下拉列表的值發送到同一頁面上的php代碼(顯示帶有複選框的列表)。請參看下面的圖片以供參考(這就是我真正想要的) -Yii2:發送數據到同一頁無法正常工作
但是,選擇從下拉列表中的一些元素後,我得到的結果,但與整個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。
我越來越喜歡這個