2

我用 JavaScript 编写了这个(完整的代码)代码。

在第 6 行中,我编写了代码:

div_new = div_new+"<form name='"+name+"' onsubmit="+ return refresh(name, url, document.forms[name][name].value , '', '', 'refresh'); +" method='post' style='display:inline;'>";

当我添加此代码时,页面无法正常工作。

我相信我的引号有问题onsubmit,因为当我删除“onsubmit”时,一切正常。

如果我用普通的 HTML(而不是 JS)编写完整的代码,一切都会很好。

我应该怎么做才能解决问题?

完整代码:

function chat(mem_id) {
    count++;
    var div_new = "<div style='position:absolute; width:200px; bottom:0px; left:10px;'>";
        div_new = div_new+"<div style='width:100%; background-color:green;'>top "+ mem_id +" - "+count+"</div>";
        div_new = div_new+"<div style='width:100%; height:300px; background-color:pink;'>bottom // refresh_chat"+ mem_id +" ";
    var name = "send_chat_"+mem_id;
    var url = "send_chat.php";
        div_new = div_new+"<form name='"+name+"' onsubmit="+ return refresh(name, url, document.forms[name][name].value , '', '', 'refresh'); +" method='post' style='display:inline;'>";
        div_new = div_new+"<textarea name='"+name+"' autofocus=autofocus placeholder='"+name+"'></textarea><input type=submit value=' send '>";
        div_new = div_new+"</form>";
        div_new = div_new+"</div>";
        div_new = div_new+"</div>";
    bottom = div_new;
    document.getElementById('bottom').innerHTML = bottom;
}
4

2 回答 2

2

请在此处查看演示

这行有几个错误

div_new = div_new+"<form name='"+name+"' onsubmit="+ return refresh(name, url, document.forms[name][name].value , '', '', 'refresh'); +" method='post' style='display:inline;'>";

使用此代码

function chat(mem_id) {
    count++;
    var div_new = "<div style='position:absolute; width:200px; bottom:0px; left:10px;'>";
        div_new = div_new+"<div style='width:100%; background-color:green;'>top "+ mem_id +" - "+count+"</div>";
        div_new = div_new+"<div style='width:100%; height:300px; background-color:pink;'>bottom // refresh_chat"+ mem_id +" ";
    var name = "send_chat_"+mem_id;
    var url = "send_chat.php";
        div_new = div_new+"<form name='"+name+"' onsubmit=\"return refresh(" + name +","+ url+",document.forms[name][name].value , '', '', 'refresh');\" + method='post' style='display:inline;'>";
        div_new = div_new+"<textarea name='"+name+"' autofocus=autofocus placeholder='"+name+"'></textarea><input type=submit value=' send '>";
        div_new = div_new+"</form>";
        div_new = div_new+"</div>";
        div_new = div_new+"</div>";
    bottom = div_new;
    document.getElementById('bottom').innerHTML = bottom;
}

主要问题是由于双引号,返回不是字符串的一部分,他需要使用转义序列

这是正确的解决方案

div_new = div_new+"<form name='"+name+"' onsubmit=\"return refresh(" + name +","+ url+",document.forms[name][name].value , '', '', 'refresh');\" + method='post' style='display:inline;'>";

我还在这里添加了相同的转义序列

于 2013-08-15T18:29:22.537 回答
0

你是对的,问题是引号。当您编写onsubmit="时,final"是关闭您开始的字符串div_new+。解决方案是转义引号字符。JavaScript 允许您使用以下\字符执行此操作:\". 所以这就是该行:

div_new = div_new+"<form name='"+name+"' onsubmit=\"+ return refresh(name, url, document.forms[name][name].value , '', '', 'refresh'); +\" method='post' style='display:inline;'>";
于 2013-08-15T18:30:01.090 回答