2012-05-17 127 views
1

像这是我的表单的选择框如何通过javascript传递选择值?

<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这个,但是它的通过值为custid,在podetfill.php页面上,并且获得了e rror

Error: custid not defined 
+3

的CustID的本地范围的给函数findcustid(上图)增加下面。要使用它,你需要在函数之外声明var custid。即全球。 – verisimilitude

+0

你的代码看起来好多了,现在它的格式正确 - 更容易阅读 - 甚至可以帮助你解决你的问题...我的提示 - 总是缩进你的代码([这里有方便的HTML/JS美化](http: //jsbeautifier.org/)) – ManseUK

回答

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 

你还需要给selectid属性使用此代码:

<select id="custid" name="cutid" onchange="findcustid(this.value)"> 
+0

你是伟大的人。 +1 – ymutlu

+0

它似乎工作,但每次只传递值0,现在传递类似1001,1002等等的值,并且它的传递值为0,导致默认选项值为0 <选项值=“0”>选择客户 – Jaiff

+0

@Jaiff您正在执行你的代码使用'window.load'事件 - 它总是会发送默认值 - 也许你应该从select上的'change'事件调用这个方法 – ManseUK

1

将声明var custid移到该函数之外。 dochange功能不可见

+0

当我在外面声明它没有得到错误,但通过custid的值=“undefined” – Jaiff

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/

+0

它会....但它是如何与OPs问题有关? – ManseUK

+0

我没有意识到有问题的范围问题。这是我的解决方案。顺便说一句,这是OP? – ymutlu

+0

查看我的答案 - 问题解答和解决方案 – ManseUK

-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> 
0

您的ajax请求在窗口加载时发送(通过window.onLoad = do改变('proid',-1);)当你还没有进行customerid选择,因此它是未定义或null。

改为在您的选择上发送您的ajax请求。

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

和dochange函数的开头作为ManiseUK所述

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

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