由 jQuery 添加的什么都不发送,javascript,php,jquery,html,forms"/>

首页 > 解决方案 > 由 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'];

但是没有文件被发送。我应该怎么办?

标签: javascriptphpjqueryhtmlforms

解决方案


感谢你们。为了以防万一,丢掉了所有的桌子,现在它可以工作了。

<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:&nbsp;<input type='text' name='courseName' /></p>
            <p>Resource:&nbsp;<input type='file' name='file' /></p>
            <p>Dependencies:&nbsp;<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的代码。


推荐阅读