dqb78642 2011-03-01 16:12
浏览 88
已采纳

PHP / AJAX - 动态文本框值

Im having trouble trying to figure out some code someone has suggested and was hoping someone could explain it a bit more, or suggest something easier.

I have 3 textboxes which i need to fill with dynamic data from a MySQL database, which data is shown is dependant on what the user clicks in a select dropdown box.

The code i have is the following:

<script type="text/javascript">
 jQuery(function( $ ){    
  $("select").change(function(e) {  
   $(".temp").load(\'index.php?parameter1=\'+ $(this).val(), , function() {
    $(".textbox1").val($(".temp1").text());
   });
  });
 });
</script>

Im not quite sure how to use javascript so im unsure of how this works, and also what code would go into the index.php file?

Any help would be greatly appreciated.

Cheers

  • 写回答

2条回答 默认 最新

  • dqqt31923 2011-03-01 16:30
    关注

    Yea this is from me and it is quite simple.

    First 2 lines are just initialisation, no need to change ever.

    3rd is a change trigger on "select", so all code inside this function will be executed when a selectbox changes its value. Using a class or id (.x, #x) would be better but in you example was none given.

    4th Line loads the content of the given url in that case eg: "index.php?parameter1=USA" into an element (div or whatever) that has the class temp.

    Finally in the 5th line this temporarly loaded content will be copyed in the textbox with the class texbox1.

    For this to work you need to include the jquery file, sth like this:

    <script src="jquery-latest.js" type="text/javascript"></script>
    

    Also there has to be: the given select, a textbox with the class textbox1, and a div (hidden) with the class temp

    In the index.php (or name it textboxvalues.php .. whatever) you just have to echo the values that you want to have in the textbox. You can use a switch or if for the parameters (for the differen textboxes) and get the values from a database or however you want. You can even use a different php file for each textbox. Whatever you want.

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

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计