2012-05-17 54 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);