Concatenate javascript to a string/parameter in a function
I am using kottke.org's old JAH example to return some html to a div in a webpage. The code works fine if I use static text. However I need to get the value of a field to add to the string that is getting passed as the parameter to the function.
var xmlhttp=false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
xmlhttp = false;
}
}
@end @*/
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
xmlhttp = new XMLHttpRequest();
}
function getMyHTML(serverPage, objID) {
var obj = document.getElementById(objID);
xmlhttp.open("GET", serverPage);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
obj.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
And on the page....
<a href="javascript://" onclick="getMyHTML('/WStepsDE?open&category="+document.getElementById('Employee').value;+"','goeshere')">Change it!</a></p>
<div id ="goeshere">Hey, this text will be replaced.</div>
It fails (with the help of Firebug) with the getMyHTML call where I try to get the value of Employee to include in the first parameter. The error is "Unterminated string l开发者_如何学Pythoniteral". Thx in advance for your help.
You're quotes are wrong. You can't nest the double-quotes inside the single-quotes. Try something like this:
<a href="javascript://" onclick="getMyHTML('/WStepsDE?open&category='+document.getElementById('Employee').value,'goeshere')">Change it!</a></p>
You've mixed the quotation marks in your onclick
handler. You can rewrite it as a separate function correct them in the following manner:
<a href="javascript://" onclick="getMyHTML('/WStepsDE?open&category='+document.getElementById('Employee').value,'goeshere')">Change it!</a>
精彩评论