我希望某些变量的值显示在字符串中。
function test()
{
var first = document.getElementById('first').value; //value ==hello
var second = document.getElementById("second").value; //value == bye
var third = document.getElementById("third").value; //value == crazy
var forth = document.getElementById("forth").value; // value == night
var myString = " \
<script> \
var firstValue = "\" + first + \""; \
var secondValue = "\" + second + \""; \
var thirdValue = "\" +third + \""; \
var forthValue = "\" +forth + \""; \
<\/script> ";
我想要显示字符串:
<script>
var firstValue = " hello ";
var secondValue = " bye ";
var thirdValue = " crazy ";
var forthValue = " night ";
</script> ";
答案 0 :(得分:3)
javascript中的变量名称不能以数字开头。
所以改变你的变量名。
var name1 = document.getElementById('1').value;
应该会更好。
答案 1 :(得分:0)
要将任何值转换为字符串,只需使用空字符串(例如 - ""+document.getElementById('1').value
答案 2 :(得分:0)
我认为你的问题只归结为字符串中的错误转义,你应该只通过代码突出显示来识别:
var myString = " \
<script> \
var firstValue = \"" + first + "\"; \
var secondValue = \"" + second + "\"; \
var thirdValue = \"" +third + "\"; \
var forthValue = \"" +forth + "\"; \
<\/script> ";
你总是逃避你想要保留在你的字符串中的字符,而不是那些在定义中具有句法意义的字符!