javascript - 由 jQuery 添加的什么都不发送
问题描述
我正在尝试一个带有 HTML 表单的小页面,在其中,jQuery 添加了附加名称的文件字段,[]
因此 PHP 目标将其作为文件数组接收。但是 PHP 没有接收到文件。
一个样品:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("#add").click(function() {
$("#deps").before("<tr id=\"dependency\"><td>Dependency:</td><td><input type=\"file\" name=\"deps[]\" /></td></tr>");
});
$("#rem").click(function() {
$("#dependency").remove();
});
});
</script>
<table>
<tr>
<td>
<button id="add">+ Dependency</button>
</td>
<td>
<button id="rem">- Dependency</button>
</td>
</tr>
<form method="POST" enctype="multipart/form-data" action="target.php">
<tr id="deps">
<td></td>
<td><input type="submit" name="submit" value="send" /></td>
</tr>
</form>
</table>
在 target.php 中:
$deps = $_FILES['deps'];
但是没有文件被发送。我应该怎么办?
解决方案
感谢你们。为了以防万一,丢掉了所有的桌子,现在它可以工作了。
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<form method="POST" enctype="multipart/form-data" action="target.php">
<p>Course Name: <input type='text' name='courseName' /></p>
<p>Resource: <input type='file' name='file' /></p>
<p>Dependencies: <button type='button' id='add'>+</button></p>
<div id="deps">
<p><input type="submit" name="submit" value="send" /></p>
</div>
</form>
</body>
</html>
脚本.js 是:
$(document).ready(function() {
$("#add").click(function() {
$("#deps").prepend("<p><input type='file' name='deps[]' /><button type='button' class='rem'>- </button></p>");
});
$(document).on("click",".rem",function() {
$(this).parents("p").remove();
});
});
基本上改变了imvain2的代码。
推荐阅读
- python - 使用 SQLite 时 SQLAlchemy 中的绑定参数冲突
- javascript - 在 Ext JS 中将字段标签设置为 HTML 字符串
- algorithm - 伪代码格式
- uuid - JDK 11 生成的 UUID 版本是多少?
- angular - 如何将 Angular 项目版本从 12.2.5 降级到 12.2.1?
- c# - 为什么我的列表没有在帖子之间的 PageModel 上保持状态?
- android - 谷歌播放签名密钥
- python - 在 matplotlib 中使用 animation.ArtistAnimation() 返回一个空白
- android - 在 TelephonyCallback 中访问电话号码 - Android
- python - ModuleNotFoundError:没有名为“pandas”的模块 | `pip install pandas` & `poetry add pandas` 失败