简体   繁体   中英

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.

        <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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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