php - 发布表单不适用于 jquery 字段

标签 php jquery html dom post

我无法获取使用动态字段将变量发布到另一个脚本的表单?

编辑:脚本创建了一个新的输入字段行,但没有一个能够将变量发布到 php 脚本。

j查询:

<script type="text/javascript">
    $(function(){

            var newRowNum = 2;


            $('#addnew').click(function(){
                    // increment the counter
                    newRowNum += 1;

                    var addRow = $(this).parent().parent();


                    var newRow = addRow.clone();


                    $('input', addRow).val($(this).val());
                    $('name', addRow).val('os' + newRowNum);


                    $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');


                    $('input', newRow).each(function(i){
                            var newID = 'os' + newRowNum + i;
                            $(this).attr('id',newID).attr('name',newID);
                    });



                    addRow.before(newRow);


                    $('a.remove', newRow).click(function(){
                            $(this).parent().parent().remove();
                            return false;                           
                    });


                    return false;
            });
    });

HTML:

<tr>
<td>
<input name="os21" type="text" id="os21" value="" size="24" maxlength="60" /></td>
<td>
<input name="os22" type="text" id="os22" value="" size="24" maxlength="60" /></td>
<td>
<input name="os23" type="text" id="os23" value="" size="10" maxlength="60" /></td>
<td><a id="addnew" href="">Add +</a></td>
</tr>
<tr><td colspan="3" align="left" style="text-align: right; padding-top: 10px;">
<input type="submit" value="Update">
</td></tr>

最佳答案

<script language="text/javascript">
    function postMyForm()
    {
        $.ajax({
            url: "url from your file to post to",
            data: $('#myForm').serialize(),
            type: 'post',
            success: function(data) {
                // do stuff when request is done
            }
        });
    }

    function AddNewRule() {
        // get last ID
        // convert to a int to count with it
        var LastID = parseInt( $('#myTable tr:last td input').attr('id').replace(/os/gi, '') );
        // add 1 to get next ID
        LastID += 1; // OR LastID = LastID + 1;  what you wanna use

        // create new rule and set the next ID
        var newRule = $('<tr><td><input name="os'+ LastID +'" type="text" id="os'+ LastID +'" value="" size="10" maxlength="60" /></td></tr>');

        // append / insert at the end of your table
        $('#myTable').append(newRule);
    }
</script>

<form id="myForm">
<table id="myTable">
    <tr>
        <td>
            <input name="os21" type="text" id="os21" value="" size="24" maxlength="60" />
        </td>
    </tr>
    <tr>
        <td>
            <input name="os22" type="text" id="os22" value="" size="24" maxlength="60" />
        </td>
    </tr>
    <tr>
        <td>
            <input name="os23" type="text" id="os23" value="" size="10" maxlength="60" />
        </td>
    </tr>
</table>
    <a id="addnew" onclick="AddNewRule();"  href="javascript:void(0);">Add +</a>
<table>
    <tr>
        <td colspan="3" align="left" style="text-align: right; padding-top: 10px;">
            <input type="submit" onclick="postMyForm();" value="Update">
        </td>
    </tr>
</table>
</form>

关于php - 发布表单不适用于 jquery 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4647552/

相关文章:

HTML/CSS Div 在浏览器调整大小时移动

javascript - Safari createPattern 失败并显示 SVG : suggestions for alternatives?

html - 一线伪元素不适用于 p 元素

php - XAMPP PhpMyAdmin 错误 1045, "Access denied for user ' root' @'localhost'

php - SQL查询中值为空时如何忽略AND字段=值?

php - 如何使用 PHP 代码从 PHPMYADMIN 获取多行

javascript - 向下滚动时显示/删除类(class)

javascript - jQuery jCorner 插件无法工作 - 我缺少什么

php - 如何从 POP3 电子邮件中提取数据?

javascript - 在灯箱中使用键盘导航/将 'onkeypress' 识别为 'onclick'