我想要做的是,如果密钥的值与php代码匹配,我希望它在选项中添加一个选定的属性。是否有可能合作php和jquery?
$('#vessel_sub_category_id').append("<option value=" + key + " "if(key == <?php echo $select_update['vessel_sub_category_id']; ?>){'selected';}" >" + value + "</option>");
答案 0 :(得分:3)
是的。但是你需要在PHP中回显字符串的引号:)
$('#vessel_sub_category_id').append('<option value="' + key + '"' + if(key == '<?php echo $select_update['vessel_category_id']; ?>'){' selected';}+' >' + value + '</option>');
甚至可以采用更聪明的方法来实现这一目标:
看起来像这样:
$('#vessel_sub_category_id').append('<option value="' + key + '"'+ (key == '<?=$select_update['vessel_category_id'];?>'?' selected':'')+' >' + value + '</option>');
更新:在if和之后
之前忘记了+