【问题标题】:Target correct form input values, while using multiple forms on same the page -PHP AJAX JQUERY定位正确的表单输入值,同时在同一页面上使用多个表单-PHP AJAX JQUERY
【发布时间】:2012-03-04 17:38:31
【问题描述】:

我有一个由 php 函数生成的表,其中每一行都包含一个操作按钮,该按钮实际上是一个包含隐藏数据的表单。我希望能够在单击操作按钮时将隐藏输入值中的数据传递给 AJAX 调用。

这是我目前所拥有的:(无论用户是否点击第二个表单操作按钮,此当前代码都是从第一个表单中选择数据)

包含表单的表格

<table class="table">
    <thead>
        <tr>
            <th>Card Type</th>
            <th>Name on Card</th>
            <th>Number</th>
            <th>Expires</th>
            <th>CVC</th>
            <th>Action</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>VISA</td>
            <td>testname testlname</td>
            <td>xxxxxxxxxxxx9999</td>
            <td>12 / 15</td>
            <td>123</td>
            <td>
                <form method="post" class="scc">
                    <input type="hidden" value="VISA" name="scc_ccType1" id="scc_ccType1">
                    <input type="hidden" value="testname testlname" name="scc_ccOwner1" id="scc_ccOwner1">
                    <input type="hidden" value="4444777711119999" name="scc_ccNumber1" id="scc_ccNumber1">
                    <input type="hidden" value="12" name="scc_ccExpiresMt" id="scc_ccExpiresMt">
                    <input type="hidden" value="15" name="scc_ccExpiresYr" id="scc_ccExpiresYr">
                    <input type="hidden" value="123" name="scc_ccCVC1" id="scc_ccCVC1">
                    <input type="submit" value="Select Card" name="select_scc" id="select_scc">
                </form>
            </td>
        </tr>
        <tr>
            <td>VISA</td>
            <td>testname testlname</td>
            <td>xxxxxxxxxxxx1111</td>
            <td>12 / 15</td>
            <td>123</td>
            <td>
                <form method="post" class="scc">
                <input type="hidden" value="VISA" name="scc_ccType1" id="scc_ccType1">
                <input type="hidden" value="testname testlname" name="scc_ccOwner1" id="scc_ccOwner1">
                <input type="hidden" value="4444555566661111" name="scc_ccNumber1" id="scc_ccNumber1">
                <input type="hidden" value="12" name="scc_ccExpiresMt" id="scc_ccExpiresMt">
                <input type="hidden" value="15" name="scc_ccExpiresYr" id="scc_ccExpiresYr">
                <input type="hidden" value="123" name="scc_ccCVC1" id="scc_ccCVC1">
                <input type="submit" value="Select Card" name="select_scc" id="select_scc">
                </form>
            </td>
        </tr>
    </tbody>
</table>

Jquery 代码

<script defer="defer" type="text/javascript">
$(document).ready(function() {
    $("#select_scc").live("click", function() {

        var postData = {
        'authorize'     : 2 ,
        'cc_type'                       : $("#scc_ccType1").val(),
        'cc_number'                     : $("#scc_ccNumber1").val(),
        'cc_expdate_month'              : $("#scc_ccExpiresMt").val(),
        'cc_expdate_year'               : $("#scc_ccExpiresYr").val(),
        'cc_security_code'              : $("#scc_ccCVC1").val(),
        'owner'                         : $("#scc_ccOwner1").val(),

        };

        $.ajax({
                url: "<?php echo base_url().'admin/creditcard';?>",
                type:'POST',
                data: postData,
                dataType: 'json',
                success: function(scard){
                    alert(scard);
                }
        });

        return false;
    });
});
</script>

【问题讨论】:

  • 不是最优雅的解决方案,而是更改其中一个表单上的 ID,ID 必须是唯一的,并且不能在同一个文档中使用两次。然后只需添加第二个ID所需的JS。此外,如果不推荐使用 jQuery 1.7+ .live() 并应替换为 .on() 并且取决于您打算做什么,您可能应该 .serialize() 您的表单而不是您现在正在做的事情?跨度>
  • 我不会在该页面的任何地方以纯文本形式放置信用卡号或 CVC 代码。这只能从后端的数据库中查询,希望是加密的。
  • 感谢您的评论,这是后端,所有数据都已加密,将 ID 更改为类,这是我的错误。序列化后如何推送新的数组键和值?

标签: php codeigniter jquery


【解决方案1】:

首先 ids 需要是唯一的,所以也许改变生成它们的 php 以添加“select_scc”作为一个类。

然后像这样做一些事情:

$('submit#select_scc').on('click', function() {
    var $this = $(this); // caches $(this), which is the select_scc element.

    // Now to get each value for the form that the user clicked on.
    var postData = {
           'cc_type' : $this.siblings('#scc_ccType1').val(),
           'cc_number' : $this.sibling('#scc_ccNumber1').val(),
           // ........ etc
        };

        // now just run the ajax function as you are doing.
});

如果用户单击时没有生成“select_scc”,则此单击函数可能会出现问题,然后将该函数绑定到已经存在的元素。例如,假设生成的所有内容都在一个名为“#container”的预定义 div 中,然后像这样编写 click 函数:

$('#container').on('click', 'submit#select_scc', function () {
});

.live 已弃用,仅指向 .on 函数,因此直接使用 .on 会节省一些加载时间。

【讨论】:

  • 感谢您的回答,我收到一条错误消息,提示 $this.siblings 不是函数
  • 您是否先缓存了 $this?请参阅我的代码中的第 2 行。除此之外,我看到我在“cc_number”上打错字了,应该是 .siblings() 而不是 .sibling()。
【解决方案2】:

这是一个更简单的方法,使用表单类作为选择器,ID 无关紧要。使用 serialize() 无需手动创建整个数据对象。

live() 已弃用,但我不知道您使用的是什么版本的 jQuery,所以我现在坚持使用它

$(function(){

$('form.scc').live('submit',(function(){
    var postData = $(this).serialize();
    $.ajax({
            url: "<?php echo base_url().'admin/creditcard';?>",
            type:'POST',
            data: postData,
            dataType: 'json',
            success: function(scard){
                alert(scard);
            }
    });

    return false;
});

});

【讨论】:

  • 感谢您的回答,如果我想在这个 postData 数组中包含一些硬编码的数组键和值,在使用 serialize() 之后我该怎么做? ???
  • 简单的方法是对它们进行urlencode并连接到postData...查看序列化结果api.jquery.com/serialize
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 2017-05-18
  • 2023-03-20
  • 2021-03-14
  • 1970-01-01
相关资源
最近更新 更多