首页  >  问答  >  正文

使用PHP循环的选择选项

我有下面的代码,该代码可以工作,但只有在单击选择选项后才能工作,但是当我在使用向上和向下箭头时更改值时,代码不起作用。

我尝试修改脚本,将选项“单击”更改为“更改”,但此解决方案不起作用。有人可以帮助我吗?

$select = $db_connect -> query("SELECT * FROM templates");      
if($select -> num_rows > 0)
{
    echo '<select id="subject" name="subject" class="form-select">';
    
    while($row = $select -> fetch_assoc())
    {
        echo '<option id="'.$row["id"].'" value="'.$row['template_subject'].'">'.$row['template_subject'].'</option>';
        ?>
        <script>
        $("#subject #<?php echo $row['id']; ?>").on('click', function(){
            
            if((this.id = "<?php echo $row['id'];?>") && (this.id != 1))
            {
                $("textarea").html("<?php echo $row['template_text']; ?>");
                $("input[name='new_subject']").hide();
            }
            else
            {
                $("textarea").html("");
                $("input[name='new_subject']").show();
            }

        }); 
        </script>
        <?php
    }
    echo '</select>';
}

P粉788765679P粉788765679209 天前323

全部回复(1)我来回复

  • P粉105971514

    P粉1059715142024-02-27 10:19:18

    您的问题出在 Javascript 代码中。

    sssccc

    从 while 循环中删除脚本,将其放在 标记之前的末尾。

    回复
    0
  • 取消回复