添加输入隐藏字段(appendTo)后,$ _POST在提交时为空

时间:2013-08-29 18:41:01

标签: php javascript jquery html forms

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

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

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

JQUERY

$('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>

1 个答案:

答案 0 :(得分: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>的直接子元素,这是不允许的。

相关问题