我有两个下拉列表,第二个是基于哪一个用户在第一个下拉列表中选择的。 我打算使用Javascript来动态更改第二个下拉列表。 但现在问题来了。 这些信息将存储在用户的个人资料中。 所以理想情况下,我希望能够将这些信息从MySQL数据库中提取出来并显示在PHP页面上。 当我从MySQl填充列表时,我将如何加载正确的动态菜单。 我怎样才能以我想要的方式完成这件事。 我不认为JavaScript会在这种情况下工作吗?
I have two Drop Down Lists, the second ones are based off which one the users selects in the first drop down. I was going to use Javascript to dynamically change the second drop down. But now here comes the problem. This information is going to be stored on a user's profile. So ideally I would like to be able to pull this information out of MySQL database and display it on a PHP page. How would I go about loading the proper dynamic menu when I populate the list from MySQl. How can I accomplish this the way I want. I dont think javascript would work in this case would it?
最满意答案
如果你不想刷新页面,那么答案就是“JavaScript”。 现在,你有几个选择。 就我个人而言,我更喜欢加载更多的前端,所以我保留一个JSON对象,概述了可能性,然后我将静态内容交换为静态内容。
这意味着onchange我会有这样的东西:
var first = document.getElementById( "id of first select" ) var items = possibilities[ first.value // you can also do things with selectedIndex and options if needs. ]; var second = document.getElementById( "id of second select" ) for( var i in items ) { var opt = document.createElement('option'); opt.setAttribute('value', items[ it ] ); second.appendChild( opt ); }您的另一种选择是通过AJAX发送“通过电话线”的数据。 没有教程的空间,我想我可以安全地将你链接到这个教程。
If you don't want a page refresh, then the answer is "JavaScript". Now, you have a couple of options there. Personally, I prefer to load more up front, so I keep a JSON object which outlines the possibilities and then I swap static content for static content.
This would mean that onchange I would have something like:
var first = document.getElementById( "id of first select" ) var items = possibilities[ first.value // you can also do things with selectedIndex and options if needs. ]; var second = document.getElementById( "id of second select" ) for( var i in items ) { var opt = document.createElement('option'); opt.setAttribute('value', items[ it ] ); second.appendChild( opt ); }Your other option is to send data "over the wire" and get it working through AJAX. Not having room for a tutorial, I think I can safely link you to this one.
更多推荐
发布评论