使用javascript动态创建的选择输入,不显示下拉菜单 [英] Dynamically created select input using javascript, not showing dropdown

查看:68
本文介绍了使用javascript动态创建的选择输入,不显示下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用javascript使用下拉列表创建了一个选择输入字段,因此用户可以添加多个条目,但是javascript无法提取用于创建选择下拉列表值的php函数.我看不到为什么下拉菜单不起作用.如果我使用php创建输入字段,则下拉列表可以正常工作.

I have created a select input field with dropdown using javascript so user can add multiple entries but the javascript does not pull in the php function used to create the select dropdown values. I am unable to see why the dropdown does not work. The dropdown does work correctly if i create the input field using php.

JavaScript

Javascript

<script>
var count = 0;
$(document).ready(function(){
  $(document).on('click', '.add', function(){
 var html = '';
 html += '<label for="category" class="col-sm-4 control-label">Business Category:</label>';
 html += '<div class="col-sm-8">';
 html += '<select name="category" class="form-control">';
 html += '<option value=""></option><?php categoryDropDown($categories); ?>';
 html += '</select>';
 html += '<button type="button" name="remove" class="w3-button w3-red w3-round w3-tiny remove" title="Remove this category"></button>';
 html += '</div>';

 $('#category').append(html) ; 
 count++;
 });

$(document).on('click', '.remove', function(){
$(this).closest('div').remove();
count--;
  });
});</script>

PHP函数

        $categories = $db->select("categories", "deleted = 0 ORDER BY name");
    function categoryDropDown($categories)
        {
            foreach($categories as $p)
            {
                $output3 .= '<option value="' . $p['id'] . '">' . stripslashes($p['name']) . '</option>';
            }
        return $output3 ;
        }

推荐答案

echo 结果.你忘了!:-P

Just echo the result. You forgot! :-P

<?php categoryDropDown($categories); ?>

<?= categoryDropDown($categories); ?>

这篇关于使用javascript动态创建的选择输入,不显示下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆