我用php生成一个简单的表单。以下代码已缩减以显示问题。
<?php
$blah = "<h1>Well</h1>"; $test = "<h2>Done</h2>";
echo '<script type="text/javascript" src="fetchlayers.js"></script>';
$formcode = "<form action=\"process.php\" method=\"post\" enctype=\"multipart/form-data \">"
. "<label for=\"file\">Filename:</label>"
. "<input type=\"file\" name=\"file\" id=\"file\"/> <br />"
. "<input type=\"submit\" name=\"submit\" value=\"Submit\" "
. "onclick=\"setTimeout(function() { sendInfo(\"$blah\", \"$test\"); },1250);\" />"
. "</form>";
echo "<h1>hello</h1>
<div id='form'>
$formcode
</div>";
这导致这个HTML代码
<h1>hello</h1>
<div id="form">
<form action="process.php" method="post" enctype="multipart/form-data ">
<label for="file">Filename:</label><input name="file" id="file" type="file">
<br><input name="submit" value="Submit" onclick="setTimeout(function() { sendInfo(" type="submit"><h1>Well</h1>", "<h2>Done</h2>"); },1250);" />
</form>
在调用sendinfo()之后,以某种方式,type = submit的表单代码结束了。为什么呢?
答案 0 :(得分:4)
这是因为你在onclick上的引用。改为使用单引号:
"onclick=\"setTimeout(function() { sendInfo('$blah', '$test'); }, 1250);\" "
现在的方式,你的onclick属性实际上是这样的:
onclick="setTimeout(function() { sendInfo("
答案 1 :(得分:0)
这似乎是一个逃避问题:
<?php
$blah = "<h1>Well</h1>"; $test = "<h2>Done</h2>";
echo '<script type="text/javascript" src="fetchlayers.js"></script>';
$formcode = "<form action=\"process.php\" method=\"post\" enctype=\"multipart/form-data \"><label for=\"file\">Filename:</label><input type=\"file\" name=\"file\" id=\"file\"/> <br /><input type=\"submit\" name=\"submit\" value=\"Submit\" onclick=\"setTimeout(function() { sendInfo('$blah', '$test'); },1250);\" /></form>";
echo "<h1>hello</h1>
<div id='form'>
". $formcode . "
</div>";
onclick中的javascript需要单引号,或者必须使用&amp; quot;。