2012-08-13 64 views
0

我想在onc​​hange事件上調用兩個JS函數,並且這兩個函數都返回一些值。它不會給我的第二個功能輸出:在onchange上調用2個JS函數並返回一些值

我的PHP代碼:

<select name="lstTypex" id="lstTypex" onchange="javascript:showCondition(this.value);showInputType(this.value);"> 
    In options some values 
    </select> 
    <span id="condition" style="padding-left:19px;"></span> 
    <span id="inputType" style="padding-left:19px;"></span> 

我的Ajax代碼:

// This function is used to submit for condition fields by ajax 
function showCondition(colName) { 

if (colName.length == 0) { 
    document.getElementById("condition").innerHTML=""; 
    return; 
} 
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
     document.getElementById("condition").innerHTML=xmlhttp.responseText; 
    } 
} 
xmlhttp.open("GET","ajax/entity_report.php?colName="+colName+"&doAction=showCondition",true); 
xmlhttp.send(); 
} 

// This function is used to submit for condition fields by ajax 
function showInputType(colName) { 

if (colName.length == 0) { 
    document.getElementById("inputType").innerHTML=""; 
    return; 
} 
if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
    xmlhttp=new XMLHttpRequest(); 
} 
else {// code for IE6, IE5 
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState==4 && xmlhttp.status==200) { 
     document.getElementById("inputType").innerHTML=xmlhttp.responseText; 
    } 
} 
xmlhttp.open("GET","ajax/entity_report.php?colName="+colName+"&doAction=showInputType",true); 
xmlhttp.send(); 
} 

我的PHP的Ajax代碼:

// include all required files for this screen 
include("../include/common.php"); 
include(MODEL . "entity_report_class.php"); 

// create class object for class function access 
$objCallUsers = new Entity_Report(); 

// call required class file functions 
$returnStr = ""; 
$arrTypex = $objCallUsers->fetch_data_type(); 
$typex = $arrTypex[$_REQUEST['colName']]; 

switch($_REQUEST['doAction']) { 
case 'showCondition': 
    $arrCondition = array(); 
    if($typex == 'TB' || $typex == 'TA') { 
     $arrCondition = array("Equal to","Not equal to","Starts with","Contains any part of word","Contains full word"); 
    } 
    else if($typex == 'DD' || $typex == 'RD') { 
     $arrCondition = array("Equal to","Not equal to"); 
    } 

    $returnStr .= '<select name="lstCondition" id="lstCondition"> 
    <option>--Please Select--</option>'; 

    foreach ($arrCondition AS $condition) { 
     $returnStr .= '<option>' . $condition . '</option>'; 
    } 

    $returnStr .= '</select>'; 

break; 

case 'showInputType': 
$arrCondition = array(); 
if($typex == 'TB' || $typex == 'TA') { 
    $returnStr .= '<input type="text" name="conditionValue" id="conditionValue" value="" />'; 
} 
else if($typex == 'DD' || $typex == 'RD') { 
    $returnStr .= ' <select name="conditionValue" id="conditionValue"></select>'; 
} 

break; 
} 

print($returnStr); 

它根據第二個函數調用給出輸出。兩個函數都返回一些東西

+0

嘗試從第一功能中調用的第二個功能,並使用一些帶有的console.log螢火蟲調試流 – 2012-08-13 07:34:46

+0

標題是ambigous - 你不返回任何值。另外,在提問時考慮正確的縮進 - 這會使其更具可讀性。 :) – 2012-08-13 08:07:42

+0

ohk ... il確保dis next tym。 – 2012-08-13 08:22:46

回答

2

您正在使用全局變量xmlhttp,第二個函數會覆蓋它。您應該通過將其與var關鍵字一起引入這兩個函數來使它在本地。

if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari 
    var xmlhttp=new XMLHttpRequest(); 
} 
else {// code for IE6, IE5 
    var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
} 
+0

嘿thanx ...它的工作.. – 2012-08-13 08:17:27