du9537 2016-04-13 16:49
浏览 43

PHP $ _POST无法读取jQuery val()设置的值

I created a little script which allows users to shadow some input values from other inputs. The JavaScript part is working fine, but now when I'm trying to send the form, it doesn't send it, saying that the field the value is copied to is empty. What's the problem here? Thanks!

$(document).ready(function() 
{
    $(":text").blur(function() 
    {
        var input = $(this);
        var id = input.attr("id");
        var fieldname = "#".concat(id);

        for (i = 0; i < fields.length; i++)
        {
            if (fields[i][4] == id)
            {
                var boxname = "#copy_".concat(fields[i][0]);
                if ($(boxname).prop("checked"))
                {
                    var fieldname2 = "#".concat(fields[i][0]);
                    $(fieldname2).val($(fieldname).val());
                }
            }
        }
    });

    $(":checkbox").change(function()
    {
        var input = $(this);
        var id = input.attr("id");
        id = id.substring(id.indexOf("_") + 1, id.length);
        var fieldname = "#".concat(id);

        var checked = input.prop("checked");
        $(fieldname).prop("disabled", checked);

        var field = FindField(0, fields[FindField(0, id)][4]);

        if (checked)
        {
            var fieldname2 = "#".concat(fields[field][0]);
            $(fieldname).val($(fieldname2).val());
        }
    });
});

function FindField(index, value)
{
    for (i = 0; i < fields.length; i++)
    {
        if (fields[i][index] == value) return i;    
    }
    return -1;
}

The form (without some irrelevant stuff):

<form method="post" action="sendform.php">
    <table>

        <?php
            foreach ($fields as $field)
            {
                if ($field[0] == "divider") echo('<tr><td colspan="2"><hr /></td></tr>');
                else 
                {   
                    switch ($field[2])
                    {
                        case FIELD_TEXT:
                        {
                            echo('<tr><td><label for="' . $field[0] . '">' . $field[1] . ': </label></td><td>');

                            if ($field[4] != "") 
                            {
                                foreach ($fields as $field2)
                                {
                                    if ($field2[0] == $field[4])
                                    {
                                        echo('<input type="checkbox" id="copy_' . $field[0] . '" title="Kopeeri väljalt &quot;' . $field2[1] . '&quot;" />');
                                    }
                                }
                            }

                            echo('<input type="text" id="' . $field[0] . '" name="' . $field[0] . '" placeholder="' . $field[1] . '" /></td></tr>');

                            break;
                        }
                    }
                }
            }
        ?>

        <tr><td colspan="2"><hr /></td></tr>
        <tr>
            <td colspan="2" style="text-align: center;">
                <input type="submit" value="Saada avaldus" name="saadaavaldus" />
            </td>
        </tr>

    </table>
</form>

A sample of the fields array:

var fields = <?php echo json_encode($fields); ?>

$fields = array
(
    //      ID/name         label/placeholder                       type            regex                                                          field copied from
    array(  "saatjanimi",   "Teie täisnimi",                        FIELD_TEXT,     "^[-,.'\s\pL]*\pL[-,.'\s\pL]\s+[-,.'\s\pL]*\pL[-,.'\s\pL]*$",   ""),
    array(  "meiliaadress", "Teie meiliaadress",                    FIELD_TEXT,     "^([\pL-.\d]+)@([\pL-.\d]+)((\.(\pL){2,63})+)$",                ""),
    array(  "isanimi",      "Lapse isa täisnimi",                   FIELD_TEXT,     "^[-,.'\s\pL]*\pL[-,.'\s\pL]\s+[-,.'\s\pL]*\pL[-,.'\s\pL]*$",   "saatjanimi")
);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stata安慰剂检验作图但是真实值不出现在图上
    • ¥15 c程序不知道为什么得不到结果
    • ¥40 复杂的限制性的商函数处理
    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题