简体   繁体   中英

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 literal". 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM