2016-06-30 41 views
0

我有一個觀點使用如何執行一個onchange事件@ Html.InputFor

@Html.InputFor(m => m.CurrentAddress.Address.Zip, new { id="currentZip", onchange = "decode_zipcode(currentZip.value,1);" }, disabled:false) 

下面的代碼和下面的JavaScript,我想在上面輸入值發生變化

var mZipArea = 0; 
var vZipCode = document.getElementById('currentZip').value; 

function decode_zipcode(vZipCode, vArea) { 
    //currentzipCode = vArea; 

     if (vZipCode.length == 5) { 
      dtdecodeZipCode(vHostUrl, vZipCode, 'return_zipdecode'); 

    } 

} 

    function return_zipdecode(vCity, vCounty, vState) { 
    if (mZipArea == 1) { 
     document.getElementById('currentCity').value = vCity; 
     document.getElementById('currentState').value = vState; 
     document.getElementById("currentCounty").value = vCounty; 
     document.getElementById('currentState').value = "United States"; 
    } 
執行

基本上這是做輸入(郵政編碼)並返回城市,州,縣和國家。當我執行代碼時,我沒有得到任何錯誤,但代碼從不執行JavaScript。我在MVC很新和JavaScript所以任何幫助,將不勝感激

編輯:外部JS文件

<script src="/_scripts/dtAddress_v10_r1.js"> 
 
    
 
     function dtaddAddressOption(selectbox, text, value) { 
 
      var optn = document.createElement("OPTION"); 
 
      optn.text = text; 
 
      optn.value = value; 
 
      selectbox.options.add(optn); 
 

 
     } 
 

 
     function dtCountryLookup(vHostUrl,vCountry, oState, vCallBack, vUseAbbr) { 
 

 
     
 

 
      // will return the callback of the state array and proper lables 
 
      //return should look like this 
 
      // myCallBackFunction(vCountyLabel,vCityLabel,vStateLabel,vZipLabel) 
 

 
      var vCountyLabel = "" 
 
      var vCityLabel = "" 
 
      var vStateLabel = "" 
 
      var vZipLabel = "" 
 
      var vBlank = "" 
 

 
      if (vUseAbbr == null) 
 
      { 
 
       vUseAbbr = false; 
 
      } 
 

 

 

 

 
      if (vCountry == 'Canada') { 
 
       // hide the county 
 
       vCountyLabel = "none" 
 
       vCityLabel = "City" 
 
       vStateLabel = "Province" 
 
       vZipLabel = "Postal Code" 
 
       vBlank = "Select Province" 
 
      } 
 

 
      if (vCountry == 'Mexico') { 
 
       // hide the county 
 

 
       vCountyLabel = "none" 
 
       vCityLabel = "Division/City/Town" 
 
       vStateLabel = "Province" 
 
       vZipLabel = "Postal Code" 
 
       vBlank = "Select Province" 
 

 
      } 
 

 
      if (vCountry == 'United States') { 
 
       // hide the county 
 
       
 
       vCountyLabel = "County" 
 
       vCityLabel = "City" 
 
       vStateLabel = "State" 
 
       vZipLabel = "Postal Code" 
 
       vBlank = "Select State" 
 

 
      } 
 

 
      if (vCountry == '') { 
 
       vCountry = 'United States' 
 
       // hide the county 
 
       vCountyLabel = "County" 
 
       vCityLabel = "City" 
 
       vStateLabel = "State" 
 
       vZipLabel = "Postal Code" 
 
       vBlank = "Select State" 
 

 

 
      } 
 

 
      //get state list 
 
      var oXML = new dtxmlObject 
 
      oXML.createXMLDocument("dealtrace") 
 
      oXML.createXMLRecord("dtrequest") 
 
      oXML.setAttributeValue("action", "901") 
 
      oXML.createXMLRecord("dtrequest_parameter") 
 
      oXML.setAttributeValue("country", vCountry) 
 

 

 
      /////////////////////////////////////////// 
 
      //// POST 
 
      ////////////////////////////////////////// 
 
      var objHttp = oXML.createHttpObject() 
 
      var vReturn = "" 
 
      objHttp.open("POST", vHostUrl + "/pages/ajax/dt_ajaxservice_open.aspx", 1) 
 
      objHttp.onreadystatechange = function() { 
 
       if (objHttp.readyState == 4) { 
 

 
        vReturn = objHttp.responseText; 
 

 
        oXML.destroyXML() 
 
        oXML.loadXML(vReturn) 
 
        oXML.loadXMLRecords("table") 
 
        var vRecCount = oXML.xmlRecordCount() 
 
        oState.length = 0 
 
        dtaddAddressOption(oState, vBlank, "") 
 
        for (var i = 0; i < vRecCount; i++) { 
 
         if (oXML.loadXMLRecord(i) == true) { 
 

 
          if (vUseAbbr == true) { 
 
           dtaddAddressOption(oState, oXML.getAttributeValue("stateabbr"), oXML.getAttributeValue("stateabbr")) 
 
          } 
 
          else { 
 
           dtaddAddressOption(oState, oXML.getAttributeValue("statename"), oXML.getAttributeValue("stateabbr")) 
 
          } 
 
         } 
 
        } 
 
        oXML.destroyXML() 
 

 
        if (vCallBack != "") { 
 
         vCallBack = vCallBack + "('" + vCountyLabel + "','" + vCityLabel + "','" + vStateLabel + "','" + vZipLabel + "')" 
 
         eval(vCallBack); 
 
        } 
 

 
       } 
 
      } 
 
      objHttp.setRequestHeader("Content-Type", "text/xml") 
 
      objHttp.send(oXML.getXML()) 
 

 
      ////////////////////////////////////////// 
 
      /// END POST 
 
      ///////////////////////////////////////// 
 

 
//   // SUBMIT TO SERVER 
 
//   var objHttp = oXML.createHttpObject() 
 
//   var b 
 
//   var vReturn = "" 
 
//    
 
//   b = objHttp.open("POST", vHostUrl + "/pages/ajax/dt_ajaxservice_open.aspx", 0) 
 
//   b = objHttp.setRequestHeader("Content-Type", "text/xml") 
 
//   b = objHttp.send(oXML.getXML()) 
 

 
//   vReturn = objHttp.responseText; 
 
//   oXML.loadXML(vReturn) 
 
//   oXML.loadXMLRecords("table") 
 
//   var vRecCount = oXML.xmlRecordCount() 
 
//   oState.length = 0 
 
//   dtaddAddressOption(oState, vBlank, "") 
 
//   for (var i = 0; i < vRecCount; i++) { 
 
//    if (oXML.loadXMLRecord(i) == true) { 
 
//     dtaddAddressOption(oState, oXML.getAttributeValue("statename"), oXML.getAttributeValue("stateabbr")) 
 
//    } 
 
//   } 
 
//   oXML.destroyXML() 
 

 
//   if (vCallBack != "") { 
 
//    vCallBack = vCallBack + "('" + vCountyLabel + "','" + vCityLabel + "','" + vStateLabel + "','" + vZipLabel + "')" 
 
//    eval(vCallBack); 
 
//   } 
 

 
     } 
 

 

 

 

 
     function dtdecodeZipCode(vHostUrl, vZipCode, vCallBack) { 
 

 

 

 
      // will return the callback of the state array and proper lables 
 
      //return should look like this 
 
      // myCallBackFunction(vCounty,vCity,vState) 
 
      var vCounty 
 
      var vCity 
 
      var vState 
 

 

 

 
      if (vZipCode.length == 5) { 
 
       var oXML = new dtxmlObject 
 
       oXML.createXMLDocument("dealtrace") 
 
       oXML.createXMLRecord("dtrequest") 
 
       oXML.setAttributeValue("action", "101") 
 
       oXML.createXMLRecord("dtrequest_parameter") 
 
       oXML.setAttributeValue("zipcode", vZipCode) 
 

 

 
       /////////////////////////////////////////// 
 
       //// POST 
 
       ////////////////////////////////////////// 
 
       var objHttp = oXML.createHttpObject() 
 
       var vReturn = "" 
 
       objHttp.open("POST", vHostUrl + "/pages/ajax/dt_ajaxservice_open.aspx", 1) 
 
       objHttp.onreadystatechange = function() { 
 
        if (objHttp.readyState == 4) { 
 

 
         vReturn = objHttp.responseText; 
 

 
         oXML.destroyXML() 
 
         oXML.loadXML(vReturn) 
 
         oXML.loadXMLRecords("table") 
 
         if (oXML.loadXMLRecord(0) == true) { 
 
          vCity = oXML.getAttributeValue("city").replace(/^\s*|\s(?=\s)|\s*$/g, ""); 
 
          vCounty = oXML.getAttributeValue("county").replace(/^\s*|\s(?=\s)|\s*$/g, ""); 
 
          vState = oXML.getAttributeValue("state").replace(/^\s*|\s(?=\s)|\s*$/g, ""); 
 
         } 
 
         oXML.destroyXML() 
 

 
         if (vCallBack != "") { 
 
          vCallBack = vCallBack + "('" + vCity + "','" + vCounty + "','" + vState + "')" 
 
          eval(vCallBack); 
 
         } 
 
        } 
 
       } 
 
       objHttp.setRequestHeader("Content-Type", "text/xml") 
 
       objHttp.send(oXML.getXML()) 
 

 
       ////////////////////////////////////////// 
 
       /// END POST 
 
       ///////////////////////////////////////// 
 

 
       //    // SUBMIT TO SERVER 
 
       //    var objHttp = oXML.createHttpObject() 
 
       //    var b 
 
       //    var vReturn = "" 
 

 
       //    b = objHttp.open("POST", vHostUrl + "/pages/ajax/dt_ajaxservice_open.aspx", 0) 
 
       //    b = objHttp.setRequestHeader("Content-Type", "text/xml") 
 
       //    b = objHttp.send(oXML.getXML()) 
 
       //    vReturn = objHttp.responseText; 
 
       //    oXML.loadXML(vReturn) 
 
       //    oXML.loadXMLRecords("table") 
 
       //    if (oXML.loadXMLRecord(0) == true) { 
 
       //     vCity = oXML.getAttributeValue("city").replace(/^\s*|\s(?=\s)|\s*$/g, ""); 
 
       //     vCounty = oXML.getAttributeValue("county").replace(/^\s*|\s(?=\s)|\s*$/g, ""); 
 
       //     vState = oXML.getAttributeValue("state").replace(/^\s*|\s(?=\s)|\s*$/g, ""); 
 
       //    } 
 
       //    oXML.destroyXML() 
 
      } else { 
 

 
          if (vCallBack != "") { 
 
           vCallBack = vCallBack + "('" + vCity + "','" + vCounty + "','" + vState + "')" 
 
           eval(vCallBack); 
 
          } 
 

 

 
      } 
 

 

 
     } 
 
    
 
</script>

+0

'.InputFor()'是你自己的擴展名嗎?你呈現的html是什麼樣的? – Jasen

+0

是的,這是我們的擴展,它與TextBoxFor相同。我會盡快爲您呈現HTML html – MaximusPrime

+0

當我看到呈現的HTML與我們關聯的外部js文件。我編輯帖子以包含外部js文件 – MaximusPrime

回答

0

嘗試使用Html.TextBox我想這和它作品。

@Html.TextBox("txtTime" , value : null , htmlAttributes: new { @class = "form-control", onchange="alert(value);"}) 
相關問題