douchu4048 2014-08-28 14:23
浏览 211
已采纳

如何使用onchange根据所选值显示不同的信息?

I have created a drop down list with names of books. When a book name is selected from the drop down, I would like it to display information about the book in the same page. The book information is stored in a mysql database. I am currently able to display information for all books, but I would like to be able to sort through the information on a php page.

I attempted to implement the answer found here, but nothing happened.

<select id="selected" name="selection" onchange="getselected(this)">
<option value="default">Select</option>
<option value="book1">Book Name 1</option>
<option value="book2">Book Name 2</option>
<option value="book3">Book Name 3</option>
<option value="book4">Book Name 4</option>
</select>

My script is:

<script>
function getselected(sel) {
var bookname = sel.value;  
}
</script>

To start, how do I display the selected book name on the page? For example, if Book Name 1 was selected from the drop down, a single line of text will appear displaying Book Name 1. I attempted to write the book name with this script.

<script type="text/javascript">document.write(bookname);</script>

Eventually I would like to use the variable bookname to select the information the mysql database. There are multiple entries with the same book name. SELECT * WHERE name = $bookname

  • 写回答

1条回答 默认 最新

  • drxt70655 2014-08-28 14:46
    关注

    Add the following HTML to your page

    <div id="dynamic"></div>
    

    change your script to the following

    <script>
    function getselected(sel) { 
    document.getElementById("dynamic").innerHTML = sel.options[sel.selectedIndex].text; 
    }
    </script>
    

    Hope this helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记