0

我查看了堆栈上的一些类似帖子,但找不到答案或解决方案。追加发生后,我提交表单并且 var 为空。我检查了 html,在附加输入字段后,它看起来不错:

<input type="hidden" name="imgdelete[]" value="5">

请帮忙,花了3个多小时。谢谢

查询

$('a.delete-img').bind('click',function()
{
    var imgid = $(this).attr('id');

    // add hidden field to let php know that this image was deleted
    $('<input>').attr({
        type: 'hidden',
        name: 'imgdelete[]',
        id: imgid,
        value: imgid
    }).prependTo('.tablepics tr:last');
});

HTML

<html>
<head></head>
<body>
<form action="" method="post" id="editCarForm" novalidate="novalidate" enctype="multipart/form-data">
<table border="0" cellpadding="0" cellspacing="0" class="tablepics" id="id-form">
        {foreach from=$CARIMGS item=carimg}
        <tr class="car-row">
        <td id="{$carimg.imgid}">
            <a class="fancybox" href="../{$carimg.img}">
                <img width="{$THUMB_WIDTH}" alt=""  src="../{$carimg.thumb}"/></img>
            </a>
        </td>
        <td style="padding:5px" id="{$carimg.imgid}">
            <input type="text" id="imgorder" name="imgorder[{$carimg.imgid}]" value="{$carimg.order}" size="2" maxlength="2" />
            <br><br>
            <a href="javascript:void(0);" id="{$carimg.imgid}" class="delete-img">delete</a><br>
        </td>
        </tr>
        {/foreach}      
        <tr>
        <br>
        </tr>
        <tr>
            <th>Images: </th>
            <td><input name="upload[]" type="file" accept="image/*" multiple="multiple" /></td>
        </tr>

        <tr>
            <th>&nbsp;</th>
            <td valign="top"> //this would be last one
                <input type="submit" value="" class="form-submit" />
                <input type="reset" value="" class="form-reset"  />
            </td>
        </tr>
</table>
</form>
</body>
</html>
4

1 回答 1

1

试试这个:

$('a.delete-img').bind('click',function()
{
    var imgid = $(this).attr('id');

    // add hidden field to let php know that this image was deleted
    $('<input>').attr({
        type: 'hidden',
        name: 'imgdelete[]',
        id: imgid,
        value: imgid
    }).prependTo('#editCarForm');
});

这会将隐藏字段插入到表格外的表单中。您的代码正在创建一个无效的 DOM,因为您将<input>元素作为 的直接子级<tr>,这是不允许的。

于 2013-08-29T18:52:05.897 回答