dongzhou8764 2010-10-20 10:29
浏览 46
已采纳

需要帮助jQuery克隆表单下拉列表(动态下拉列表)

I am using PHP to generate dynamic dropdown, the dropdown list items are fetched from database.

Here's the php code for that and it works.

<select name="title[]" id="title">
<?php $categories = Category::find_by_cid(5); foreach($categories as $category): ?>
<option value="<?php echo $category->name; ?>"><?php echo $category->name; ?></option>
<?php endforeach; ?>
</select>

What i want to do is, Using jQuery whenever a link with id AddNew is clicked, a new dropdown should appear below that, containing same values.

Also, is there any way to execute php code inside javascript??

ex :

<script>
var name = "<?php echo $user->name; ?>";
</script
  • 写回答

3条回答 默认 最新

  • douwei8911 2010-10-20 10:36
    关注

    To clone elements, you can use jQuery's .clone() method (obviously):

    $('#addNew').click(function() {
        $('select.title').after($('select.title').clone());
    });
    

    Note that ID's should be unique on the page, if you are going to clone the select element, give it a class instead of ID.

    And yes, you can "use" PHP in JavaScript, provided that the file is processed with PHP. Just note that you are not actually accessing PHP with JavaScript, but rather creating JavaScript files dynamically.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应