简体   繁体   English

jQuery自定义添加属性

[英]jQuery Custom add attribute

What I am trying to do is that if the value of the key matches the php code I want it to add a selected attribute in the option. 我想要做的是,如果密钥的值与php代码匹配,我希望它在选项中添加一个选定的属性。 is it possible to collaborate php and jquery? 有可能合作php和jquery吗?

    $('#vessel_sub_category_id').append("<option value=" + key + " "if(key == <?php echo $select_update['vessel_sub_category_id']; ?>){'selected';}" >" + value + "</option>"); 

Yes it is. 是的。 But you need to put quotes around string you echo in PHP :) 但你需要在PHP中回显你的字符串引号:)

$('#vessel_sub_category_id').append('<option value="' + key + '"' + if(key == '<?php echo $select_update['vessel_category_id']; ?>'){' selected';}+' >' + value + '</option>'); 

There would even be a smarter way to achieve this: 甚至有一种更聪明的方法来实现这一目标:

It would look then like this: 它看起来像这样:

$('#vessel_sub_category_id').append('<option value="' + key + '"'+ (key == '<?=$select_update['vessel_category_id'];?>'?' selected':'')+' >' + value + '</option>');

Update: forgot the + before the if and afterwards 更新:在if和之后忘记了+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM