2017-02-02 24 views
0

我正在做一個函數,檢查de url中的查詢(?regio = zuid)。現在該功能無法正常工作。 如果我把這個在控制檯jQuery函數沒有定義,但在控制檯中的功能工作

jQuery("select[title='Regio']").val(nCorrespondingVal); 

我得到nCorrespondingVal沒有定義

如果我把整個功能是否能夠正常工作

function fnSelectDropDownValue(){ 
    var nCorrespondingVal = jQuery("select[title='Regio'] > option:contains('+sRegio+')").val(); 
    jQuery("select[title='Regio']").val(nCorrespondingVal); 
} 

線從網址填充值在DropDown菜單中查詢

jQuery(document).ready(function() { 
    document.getElementById("defaultOpen").click(); 

    function getParameterByName(name, url) { 
     if (!url) { 
      url = window.location.href; 
     } 
     name = name.replace(/[\[\]]/g, "\\$&"); 
     var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), 
      results = regex.exec(url); 
     if (!results) return null; 
     if (!results[2]) return ''; 
     return decodeURIComponent(results[2].replace(/\+/g, " ")); 
    } 

    var sRegio = "Regio "+getParameterByName('regio'); 
    //output = Regio Zuid 

    function fnSelectDropDownValue(){ 
    var nCorrespondingVal = jQuery("select[title='Regio'] > option:contains('+sRegio+')").val(); 
     jQuery("select[title='Regio']").val(nCorrespondingVal); 
    } 

    }); 
+1

你沒有在代碼中調用'fnSelectDropDownValue'函數。 'jQuery(「select [title ='Regio']」)。val(nCorrespondingVal);'由於範圍問題而無法在控制檯上工作。 – acesmndr

回答

1

試試這個

把你的函數放在外面jQuery(document).ready(function(){});

jQuery(document).ready(function() { 
    document.getElementById("defaultOpen").click(); 
    var sRegio = "Regio " + getParameterByName('regio'); 
}); 
function getParameterByName(name, url) { 
    if (!url) { 
     url = window.location.href; 
    } 
    name = name.replace(/[\[\]]/g, "\\$&"); 
    var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), 
     results = regex.exec(url); 
    if (!results) return null; 
    if (!results[2]) return ''; 
    return decodeURIComponent(results[2].replace(/\+/g, " ")); 
} 
function fnSelectDropDownValue() { 
    var nCorrespondingVal = jQuery("select[title='Regio'] > option:contains('+sRegio+')").val(); 
    jQuery("select[title='Regio']").val(nCorrespondingVal); 
} 
+0

請告訴我接下來會發生什麼......... thanx –

相關問題