gpt4 book ai didi

javascript - 如何在编辑弹出窗口时在 php 中获取 ajax 响应

转载 作者:行者123 更新时间:2023-11-27 23:02:17 25 4
gpt4 key购买 nike

我的要求是当我单击编辑按钮时,然后使用 ajax 显示包含所选所有值的弹出窗口

我已发送这样的回复:

$data = array('cdid' => $model->cdid, 'cid' => $model->cid, 'icdcode' => $model->icdcode, 'category' => $model->category);
echo json_encode($data);

我在警报框中也得到了这样的响应

{"cdid":2,"cid":6,"icdcode":"6","category":2}

但问题是如何使用此数据在弹出窗口中显示选定的复选框或文本框值、下拉列表值。 enter image description here

最佳答案

嗯,可能有两种情况,即

场景 1,弹出窗口的 file.php 中有一组值,

PHP:

/* If you have json in your popup's file.php
You needs to convert the json into php array.
*/
$values_array = json_decode($json_string, true);

/* Initializing default values to the variables that will be used to make selections*/

$check_radio = $check_select = "";
if( is_array($values_array) ){
// supposing, the category is for radio buttons
$check_radio = $values_array['category'];
// supposing, the icdcode is for select box
$check_select = $values_array['icdcode'];
}

HTML:

<input type="radio" name="category" value="ICD-10" <?php echo ($check_radio=='ICD-10') ? " checked='checked'":'';?>> ICD-10<br>
<input type="radio" name="category" value="ICD-9" <?php echo ($check_radio=='ICD-9') ? " checked='checked'":'';?>> ICD-9<br>

<select id="icd-code" name="icd-code" multiple>
<option value="1001" <?php echo ($check_select=='1001') ? " selected='selected'":'';?>>1001</option>
<option value="1002" <?php echo ($check_select=='1002') ? " selected='selected'":'';?>>1002</option>
</select>

场景 2,弹出窗口的 file.php 中没有 php 值数组或 json 数组,而是 Javascript JSON 对象:

// Assuming that you have JSON data like this
// var data = {"cdid":2,"cid":6,"icdcode":"6","category":2};

// So, you will need to Parse Json like this:
// https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
var parsedData = JSON.parse(data);

// Making Checked the Radio buttons by name `category` and value
$("input[name=category][value='"+parsedData.category+"']").prop("checked",true);

//Making select options selected by option value
$('#icd-code option[value=' + parsedData.icdcode + ']').attr('selected', true);

希望它能帮助您解决问题:)

关于javascript - 如何在编辑弹出窗口时在 php 中获取 ajax 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36976525/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com