javascript - 用其复选框包装一些输入并发送到数据库

标签 javascript php html mysql

重点是“如何选择与选中的复选框同一行中的所有输入元素?”

MARK       NAME                   QUANTITY                      PRICE      
--------------------------------------------------------------------------------------
 []     inputForName1         inputForQuantity1             inputForPrice1
--------------------------------------------------------------------------------------
 []     inputForName2         inputForQuantity2             inputForPrice2
--------------------------------------------------------------------------------------
 []     inputForName3         inputForQuantity3             inputForPrice3
--------------------------------------------------------------------------------------
 []     inputForName4         inputForQuantity4             inputForPrice4
--------------------------------------------------------------------------------------
 []     inputForName5         inputForQuantity5             inputForPrice5
--------------------------------------------------------------------------------------
                                                                              [SUBMIT]

(这里“[]”是一个复选框)

当选中某些列表时,则选择其行上的所有列表(输入)。

然后,如何提交复选框,例如,如果有两个复选框被选中,那么这两个复选框中的所有输入都将发送到数据库中的表中。

请告诉我如何执行此操作。在此先感谢您的帮助。

最佳答案

对于这个 HTML 来说,这并不难:

<table>
    <thead>
        <tr>
            <th>Mark</th>
            <th>Name</th>
            <th>Qty</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td><input type="checkbox" name="id[1]" /></td>
            <td><input type="text" name="name[1]" /></td>
            <td><input type="text" name="quantity[1]" size="3" /></td>
            <td><input type="text" name="price[1]" size="3" /></td>
        </tr>
        <tr>
            <td><input type="checkbox" name="id[2]" /></td>
            <td><input type="text" name="name[2]" /></td>
            <td><input type="text" name="quantity[2]" size="3" /></td>
            <td><input type="text" name="price[2]" size="3" /></td>
        </tr>
        <tr>
            <td><input type="checkbox" name="id[3]" /></td>
            <td><input type="text" name="name[3]" /></td>
            <td><input type="text" name="quantity[3]" size="3" /></td>
            <td><input type="text" name="price[3]" size="3" /></td>
        </tr>
        <tr>
            <td colspan="4"><input id="save" type="button" value="Submit" /></td>
        </tr>
    </tbody>
</table>
<h3>Post data:</h3>
<div class="submit_data">click Submit...</div>

完整的 jQuery 代码片段是:

$('input[name^=id]').on('change', function(e) {
    var thisCheckbox = $(this);
    var thisRow = thisCheckbox.closest('tr');
    if ( thisCheckbox.is(':checked') ) {
        thisRow.addClass('row_selected');
    } else {
        thisRow.removeClass('row_selected');
    };
});
$('#save').on('click', function(e) {
    e.preventDefault();
    var toPost = $('.row_selected input').serialize();
    /* Now post and insert into database */
    $.post('insert_into_db.php', toPost, function(data) {
        alert('Success!');
    });
});

See the code in action .

在 PHP 中发布的变量是数组:

[id] => Array
    (
        [1] => on
        [3] => on
    )

[name] => Array
    (
        [1] => My name 1
        [3] => My name 3
    )

[quantity] => Array
    (
        [1] => 100
        [3] => 50
    )

[price] => Array
    (
        [1] => 23.34
        [3] => 15.23
    )

您可以这样浏览它们:

foreach( $_POST['id'] as $id=>$on ) {
    echo 'ID: ' . $id . '<br />';
    echo 'Name: ' . $_POST['name'][$id] . '<br />';
    echo 'Qty: ' . $_POST['quantity'][$id] . '<br />';
    echo 'Price: ' . $_POST['price'][$id] . '<br />';
    echo '<hr />';
};

输出:

ID: 1
Name: My name 1
Qty: 100
Price: 23.34
------------------
ID: 3
Name: My name 3
Qty: 50
Price: 15.23
------------------

请注意:在某些设备上,您通常需要关闭 jQuery 缓存:

$.ajaxSetup({ cache: false });

或发布具体内容:

$.ajaxSetup({
   type: 'POST',
   headers: { "cache-control": "no-cache" }
});

关于javascript - 用其复选框包装一些输入并发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930440/

相关文章:

php - 如何将 Quickbooks 桌面应用程序与 PHP(网络应用程序)集成?

python - 如何通过内存中的图像文件填写 Jinja2 模板中的文件输入

javascript - Knockout Js Radio Bindings 未设置值

javascript - 正则表达式删除 { } 之外的所有内容

php - 将 HTML 占位符添加到 Zend_Form 中

javascript - React Hooks 的 Keydown/up 事件无法正常工作

php - 剥离标签,但保留第一个

javascript - 通过 Javascript 更改类的不透明度

javascript - 在javascript中更改背景图像大小

javascript - 实现正则表达式以在 javascript 文件中查找带有类的 span