简体   繁体   English

php循环中的选择选项

[英]Selection option in php loop

I have the code below and this code works, but only after clicking on selection option but code doesn't work when i change value when use arrow up and down.我有下面的代码并且此代码有效,但仅在单击选择选项后,但当我使用向上和向下箭头更改值时代码不起作用。

I tried modification script, change option "click" to "change" but this solution doesn't work.我尝试了修改脚本,将选项“单击”更改为“更改”,但此解决方案不起作用。 Someone can help me ?有人可以帮助我吗?

$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>';
}

Your problem is in the Javascript code.您的问题出在 Javascript 代码中。

        <script>
        $("#subject").change( function(){
             var text = $( "#subject option:selected").val();
             var id = $(this).children(":selected").attr("id");

            if(id != 1)
            {
                $("textarea").html(text);
                $("input[name='new_subject']").hide();
            }
            else
            {
                $("textarea").html("");
                $("input[name='new_subject']").show();
            }

        }); 
        </script>

remove the script from the while loop , put it at the end before </body> tag.从 while 循环中删除脚本,将其放在</body>标记之前的末尾。

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

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