将函数参数从onchange传递给JavaScript函数

将函数参数从onchange传递给JavaScript函数

本文介绍了将函数参数从onchange传递给JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,当下拉列表框更改时,我目前正在使用AJAX更新网站的一部分,但是我无法传递参数以供该功能使用.

Okay I'm currently using AJAX to update part of my site when a drop down box is changed, but I'm having trouble passing the parameters to be usable by the function.

这是HTML:

<script type='text/javascript' src='ajax.js'></script> //Where to find function

<div id="combobox" style="width: 150px; height: 300px; overflow-y: scroll">
  <select onchange="MakeRequest(test); location = this.options[this.selectedIndex].value;">
    <option>Races</option>

    <?php ComboBox() ?>
  </select>
   .
   .

这是外部ajax.js文件中的AJAX函数:

Here is the AJAX function in external ajax.js file:

function MakeRequest(test) {
  var linkInfo = "teleport.php?call=" + test;
  var xmlHttp = getXMLHttp();

  xmlHttp.onreadystatechange = function() {
    if (xmlHttp.readyState == 4) {
      HandleResponse(xmlHttp.responseText);
    }
  }

  xmlHttp.open("GET", linkInfo, true);
  xmlHttp.send(null);
}

我将如何工作?

推荐答案

Ajax本质是异步的.我想您想先向服务器发送AJAX请求,并在响应结果上将客户端的用户重定向到另一页.

Ajax nature is asynchronous. I guess you want to first send an AJAX request to the server, and on the result of its response, redirect user on the client side to another page.

您的代码中发生的事情是,首先,调用了 MakeRequest()函数,但该函数立即返回(因为ajax是异步的),因此,的下一部分onchange 属性将被调用,并且用户将被重定向到另一个页面.

What happens in your code, is that first of all, MakeRequest() function is called, but it's immediately returned back (because ajax is asynchronouse), thus the next part of the onchange attribute would be called, and user would be redirected to another page.

要进行测试,请使您的ajax调用同步.请参见此处.

To test, make your ajax call synchronous. See here.

这篇关于将函数参数从onchange传递给JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:38