我在视图中有以下代码
@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 :(得分:0)
尝试使用Html.TextBox我试过这个并且它可以工作。
@Html.TextBox("txtTime" , value : null , htmlAttributes: new { @class = "form-control", onchange="alert(value);"})