使用javascript从echo下拉列表传递数据

时间:2018-04-26 22:26:39

标签: php

我有这个使用echo编写的下拉列表 但是选择并不像预期的那样

这是我的选择标记

 $output.=" <select name='selectnewauthor' id='selectnewauthor' class='form-control input-lg selectnewauthor'>
    <option disabled selected value>Select Author</option> ";


 // Getting data from database
 $getresourcetype = "Book";                 
 $selectqry = "SELECT author_fullname FROM tbl_author_add WHERE resource_type = ? ORDER BY author_fullname";
 $stmt_author = $mysqlconnection->prepare($selectqry);
 $stmt_author->bind_param("s",$getresourcetype);
 $stmt_author->execute();
 $stmt_author->store_result();
 $stmt_author->bind_result($author_fullname);

 // Writing options
 while($stmt_author->fetch()) {
    $author_fullname = $author_fullname;
    $output .= "<option value='{$author_fullname}'>{$author_fullname}</option>";
 }
 $output.="</select>

1 个答案:

答案 0 :(得分:0)

听起来好像你的#selectnewauthor元素是动态生成的。因此,您需要将侦听器附加到页面加载时可用的元素,并使用 event delegation

基本上,而不是$("#selectnewauthor").on('change', function() ... 您正在寻找$("document").on("change", "#selectnewauthor", function() ...