1

就像这是我表单的选择框

<select name="cutid" onchange="findcustid(this.value)">
<option value="1001">cust 1</option>
<option value="1002">cust 2</option>            
<option value="1003">cust 3</option>            
</select>

现在我有这个 ajax 代码正在传递并填充一些选择框

function findcustid(custid) {
    var custid = custid;
}

function Inint_AJAX() {
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {} //IE
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {} //IE
    try {
        return new XMLHttpRequest();
    } catch (e) {} //Native Javascript
    alert("XMLHttpRequest not supported");
    return null;
};

function dochange(src, val) {
    var req = Inint_AJAX();
    req.onreadystatechange = function () {
        if (req.readyState == 4) {
            if (req.status == 200) {
                document.getElementById(src).innerHTML = req.responseText; //retuen value
            }
        }
    };
    req.open("GET", "podetfill.php?data=" + src + "&val=" + val + "&custid=" + custid); //make connection
    req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
    req.send(null); //send value
}
window.onLoad = dochange('proid', -1); // value in first dropdown

在这条线上

req.open("GET", "podetfill.php?data="+src+"&val="+val+"&custid="+custid);

上面的代码我已经添加了这个,但它没有在页面上+"&custid="+custid传递值并出现错误custidpodetfill.php

Error: custid not defined
4

5 回答 5

2

由于以下原因,您不能使用您的方法:

  • var custid函数内声明 - 所以它只能在该函数内访问
  • 即使var custid被声明为全局变量,您仍然无法使用它,因为 AJAX 是使用window.load事件执行的,这意味着该select.change事件尚未被触发

一种解决方案是select在使用它之前获取它的值:

var cus = document.getElementById('custid');
var custid = cus.options[cus.selectedIndex].value;
req.open("GET", "podetfill.php?data=" + src + "&val=" + val + "&custid=" + custid); //make connection

您还需要提供selectanid属性才能使用此代码:

<select id="custid" name="cutid" onchange="findcustid(this.value)">
于 2012-05-17T08:20:54.870 回答
1

将您的声明var custid移到函数之外。dochange对函数不可见

于 2012-05-17T08:12:51.870 回答
0

您的 ajax 请求在加载窗口时发送(通过 window.onLoad = dochange('proid', -1);) 当您尚未选择 customerid 时,它是未定义的或为空的。

而是在您的选择上发送您的 ajax 请求。

<select name="cutid" onchange="dochange('proid', -1)" >

正如 ManiseUK 所说,在 dochange 函数的开头添加以下内容

var cus = document.getElementById('custid');
var custid = cus.options[cus.selectedIndex].value;

消除
window.onLoad = dochange('proid', -1);

于 2012-05-17T08:38:12.030 回答
0

这对我有用,下面有小提琴链接。

<select name="report_type" onchange="alert(this.options[this.selectedIndex].value);">
<option value="1001">cust 1</option>
<option value="1002">cust 2</option>            
<option value="1003">cust 3</option>            
</select>

http://jsfiddle.net/ymutlu/JVrwL/

于 2012-05-17T08:17:38.537 回答
-1
  <select name="report_type" onchange="findcustid(this.options[this.selectedIndex].value);">
     <option value="1001">cust 1</option>
     <option value="1002">cust 2</option>            
     <option value="1003">cust 3</option>            
  </select>​​​​​​​​​​​​​​​​​​​​​​​​​​


  <script lang="javascript" type="text/javascript"  >
   function findcustid(custid) {
   var custid = custid;
   //alert(custid);
   }
  </script>
于 2012-05-17T08:20:20.743 回答