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

select1つの解決策は、使用する前にの値を取得することです。

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

このコードを使用するにはselect、属性を指定する必要もあります。id

<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

これは私のために働いた、下にフィドルリンクがあります。

<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 に答える
0

まだcustomeridを選択していないため、ウィンドウがロードされたときに(window.onLoad = dochange('proid'、-1);)ajaxリクエストが送信されるため、未定義またはnullになります。

代わりに、選択した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 に答える
-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 に答える