如何更改静态下拉列表中显示的标签

时间:2016-01-25 13:31:28

标签: php html

我想更改性别的静态下拉列表,但它无效。这是我的代码:

<select id="gender" name="gender" value="<?php echo $row['gender'];?>" >
    <option value="<?php echo $row['gender'];?>" >Select Gender</option>
    <option value="male">Male</option>
    <option value="female">Female</option>
</select>

该值是在控制台中打印但不在显示为SELECT GENDER的UI中打印,但我需要使用下拉列表显示输出。

4 个答案:

答案 0 :(得分:6)

您需要使用Sort (cost=287.26..287.26 rows=1 width=8) (actual time=711.371..711.377 rows=44 loops=1) Sort Key: booking_id Sort Method: quicksort Memory: 27kB Buffers: shared hit=8 read=7437 written=1 -> Bitmap Heap Scan on booking (cost=275.25..287.25 rows=1 width=8) (actual time=711.255..711.294 rows=44 loops=1) Recheck Cond: ((date_trunc('day'::text, from_time) >= '2016-01-25'::date) AND valid AND ((type)::text = 'LESSON_SUBSTITUTE'::text) AND (substitute_confirmation_token IS NULL)) Filter: (from_time >= '2016-01-25 14:23:00.004'::timestamp without time zone) Buffers: shared hit=5 read=7437 written=1 -> BitmapAnd (cost=275.25..275.25 rows=3 width=0) (actual time=711.224..711.224 rows=0 loops=1) Buffers: shared hit=5 read=7433 written=1 -> Bitmap Index Scan on idx_booking_lesson_substitute_day (cost=0.00..20.50 rows=594 width=0) (actual time=0.080..0.080 rows=72 loops=1) Index Cond: (date_trunc('day'::text, from_time) >= '2016-01-25'::date) Buffers: shared hit=5 read=1 -> Bitmap Index Scan on booking_substitute_confirmation_token_key (cost=0.00..254.50 rows=13594 width=0) (actual time=711.102..711.102 rows=2718734 loops=1) Index Cond: (substitute_confirmation_token IS NULL) Buffers: shared read=7432 written=1 Total runtime: 711.436 ms 条件。

实施例

IF/ELSE

注意:以上示例中假设<option <?php if($row['gender'] == 'male') echo "selected";?> value="male">Male</option> <option <?php if($row['gender'] == 'female') echo "selected";?> value="female">Female</option> 的值。

您需要从$row['gender']

中删除value="<?php echo $row['gender'];?>"

答案 1 :(得分:6)

我认为你正在寻找这个:

<select id="gender" name="gender">
<option value="" >Select Gender</option>
<option <?=($row['gender'] == 'male' ? 'selected=""' : '')?> value="male">Male</option>
<option <?=($row['gender'] == 'female' ? 'selected=""' : '')?> value="female">Female</option>
</select>

<强>解释

无需使用<select>值,因为它不会返回任何内容。因此,请从select标记中删除value="<?php echo $row['gender'];?>"

如何运作?

根据您的要求,如果数据库值$row['gender']等于&#34;男性&#34;或者女性而不是选择相关选项。为此,您需要在selected=""中打印<option>,如示例中所示。

答案 2 :(得分:-2)

要获得所选值,您需要在选择框的选项中添加“已选择”属性。为此,当您渲染选项时,必须检查$ row ['gender']是否与选项值匹配并添加选中的单词,这意味着将在您的选择框中选择该选项。如果默认情况下没有选项选择属性将是选择框中的第一个选项。

<select id="gender" name="gender">
    <option>Select Gender</option>
    <option value="male" <?php echo ($row['gender'] == 'male')?'selected':''?>>Male</option>
    <option value="female" <?php echo ($row['gender'] == 'female')?'selected':''?>>Female</option>
</select>

例如:

$row['gender'] = $male;

Html输出将是:

<select id="gender" name="gender">
    <option>Select Gender</option>
    <option value="male" selected>Male</option>
    <option value="female" >Female</option>
</select>

答案 3 :(得分:-2)

<select id="gender" name="gender">
      <option  <?php if($row['gender'] !== "Select Gender") echo "selected";?> >Select Gender</option>
      <option value="male" <?php if($row['gender'] == 'male') echo "selected";?> >Male</option>
      <option value="female" <?php if($row['gender'] == 'female') echo "selected";?> >Female</option>
     </select>
      <br/>