设置类='选定'在多个选项标签中(列表/菜单)

发布于 2024-11-26 05:42:19 字数 1513 浏览 0 评论 0原文

抱歉,我想问一个新手问题:)

我有这样的PHP脚本:

<select id="member">
 <?php
  $sql   = " select id,name from member order by name ";
  $query = mysql_query($sql);
  while($row=mysql_fetch_array($query))
  {             
 ?>
  <option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
 <?php
  }
 ?>                
</select>

然后结果将生成像这样的html

<select id="member">
 <option value="1">Jon Skeet</option>
 <option value="2">Marc Gravell</option>
 <option value="3">Darin Dimitrov</option>
</select>

我的问题是,实际上我在mysql中有2个表,第一个是member表,第二个member_invited表。
成员表中:
id | 姓名
---------------
00 | 00乔恩·斯基特
01 |马克·格拉维尔
02 | Darin Dimitrov


然后在 member_invited 表中:
id | 姓名
---------------
00 | 00乔恩·斯基特
02 | Darin Dimitrov


在这两个表中 我想这样列出我的清单:
(查看成员邀请的选项标签中的 class="selected")

<select id="member">
 <option value="1">Jon Skeet</option>
 <option value="2" class="selected">Marc Gravell</option>
 <option value="3" class="selected">Darin Dimitrov</option>
</select>

请随意使用 javascript 或 jquery,或者也许我可以仅使用 PHP 来解决它?
非常感谢:)

Sorry, I want to ask a newbie question :)

I have PHP scripts like this :

<select id="member">
 <?php
  $sql   = " select id,name from member order by name ";
  $query = mysql_query($sql);
  while($row=mysql_fetch_array($query))
  {             
 ?>
  <option value="<?php echo $row["id"]; ?>"><?php echo $row["name"]; ?></option>
 <?php
  }
 ?>                
</select>

then the result will be generating html like this

<select id="member">
 <option value="1">Jon Skeet</option>
 <option value="2">Marc Gravell</option>
 <option value="3">Darin Dimitrov</option>
</select>

my question is, actually I have 2 tables in mysql, first is member table and second member_invited table.
in member table:
id | name
---------------
00 | Jon Skeet
01 | Marc Gravell
02 | Darin Dimitrov

then in member_invited table:
id | name
---------------
00 | Jon Skeet
02 | Darin Dimitrov

within this two table
I want to make my list like this:
(look at class="selected" is in option tag that member has invited)

<select id="member">
 <option value="1">Jon Skeet</option>
 <option value="2" class="selected">Marc Gravell</option>
 <option value="3" class="selected">Darin Dimitrov</option>
</select>

please feel free to using javascript or jquery,, or maybe I can solve it just by using PHP ?
many thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

缱绻入梦 2024-12-03 05:42:19

您必须在 MySQL 中连接到 member_invited 表,然后扩展有关结果集中的 is_invited 标志的 while 循环

<?php
  $sql = "SELECT x.id,x.name,IFNULL(y.id,0) AS is_invited FROM member x LEFT JOIN member_invited y ON x.id=y.id ORDER BY x.name";
  $query = mysql_query($sql);
  while($row=mysql_fetch_array($query))
  {             
?>
<option value="<?php echo $row["id"]; ?>"<?php if ($row['is_invited']>0) { echo " class=\"selected\""; } ?>><?php echo $row["name"]; ?></option>
<?php
  }
?>

you have to do it in MySQL with a join to member_invited table and then extend your while loop regarding is_invited flag from resultset

<?php
  $sql = "SELECT x.id,x.name,IFNULL(y.id,0) AS is_invited FROM member x LEFT JOIN member_invited y ON x.id=y.id ORDER BY x.name";
  $query = mysql_query($sql);
  while($row=mysql_fetch_array($query))
  {             
?>
<option value="<?php echo $row["id"]; ?>"<?php if ($row['is_invited']>0) { echo " class=\"selected\""; } ?>><?php echo $row["name"]; ?></option>
<?php
  }
?>
罪#恶を代价 2024-12-03 05:42:19

尝试这样的事情

$("#member option").each(function(){
    var $this = $(this);
   if($this.val() == "2" || $this.val() == "3"){
      $this.addClass("selected");
   }
});

Try something like this

$("#member option").each(function(){
    var $this = $(this);
   if($this.val() == "2" || $this.val() == "3"){
      $this.addClass("selected");
   }
});
花间憩 2024-12-03 05:42:19

您可能想使用类似的方法来启用多重选择:

http://www.w3schools.com/ Tags/att_select_multiple.asp

而且在服务器端做你想做的事更聪明......

You probably want to use something like this to enable multiple selection :

http://www.w3schools.com/tags/att_select_multiple.asp

And it's more clever to do what you want server-side...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文