【问题标题】:How to get the value of radio buttons so that the value can be manipulated如何获取单选按钮的值以便可以操作该值
【发布时间】:2016-01-03 16:31:59
【问题描述】:

编辑:我终于成功了!下面是我的例子。我希望这可以帮助有同样需要的其他人。

我需要一个发货单选按钮。一种用于普通运输,一种用于快递运输。

在此表单上通过谷歌搜索和 cale_b 获得一些帮助后,我的问题如下:

我有一个 foreach 循环,它从数据库中获取值并将它们显示在页面上。我需要一个单选按钮选择器(或任何以相同方式工作的东西)来选择用户想要的运输类型。我需要每行上的购买按钮来获得从另一个 php 页面计算的总数。

我可以从我的运输数据库中提取所有这些信息并进行计算并使用 ajax 将其发送回页面,但我需要能够根据每个动态获取发送和带回的价格、尺寸、类型和 shippingType排。然后使用新的总成本并将其分配给一个名为 totalCost 的输入字段以进行提交。

这是我现在使用的 html/php 代码。

 <div class="details">
    <table class="default">
        <th>Type</th>
        <th>Size</th>
        <th>Price</th>
        <th>Shipping</th>
        <th>Total</th>
        <th>Buy</th>

    <?php
        $getArtDetails = get_tableContentsWhere($con,'prices','artID',$id);
        foreach($getArtDetails as $detail)
        {

    ?>
        <tr>
            <td class="type"><?= $detail['type'] ?></td>
            <td class="size"><?= $detail['size'] ?></td>
            <td class="price">$<?= $detail['price'] ?>.00</td>
            <td>
                <input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="Xpress">Express Shipping
                <input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="Regular">Regular Shipping
                <input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="Free">Free Shipping
            </td>
            <td class="shipping"></td>
            <td>
                <form action="" method="post">
                    <input type="hidden" name="totalCost" value="set by jquery?">
                    <input type="submit" name="submit" value="Buy">
                </form>                                                        
            </td>
        </tr>
    <?php } ?>


    </table>

</div>

这是返回我的总成本的 getShipping.php 文件。

shipping.php:

<?php
include 'admin/config/functions.php';

$size = $_GET['size'];
$type = $_GET['type'];
$price = $_GET['price'];
$shippingType = $_GET['shippingType'];

$shipping = get_shippingRate($con,$size,$type,$shippingType);

$totalCost = $shipping + $price;

echo $totalCost;

以下脚本完成了我需要它做的事情,但有 2 个问题 1.当我选择一个单选按钮时,它们的值都会改变,而不是每一个。 cale_b 我​​在这里尝试了您的解决方案以查找下一个 TR,但它不返回结果。反过来也不设置提交的输入 2. 另一个问题是我需要它将尺寸、类型和价格动态发送到我的 getShipping.php 页面。我也不知道该怎么做。

<script>
$('input[name^="shipping"]').each(function(index) {
    $(this).on('click', function(){
        var shippingType = $(this).val();
        var size = $(this).closest('tr').find('td.size').text();
        var type = $(this).closest('tr').find('td.type').text();
        var price = $(this).closest('tr').find('td.price').text();
        // I had to set $(this) to equal $this so I could access it within the ajax call as $(this) was referring to itself.
        var $this = $(this);


        $.ajax({
            type: "GET",
            url: "getShipping.php",
            data: 'size=' + size + '&type=' + type + '&shippingType=' + shippingType + '&price=' + price,
            success:function(data){
                //called when successful
                $this.closest('tr').find('td.shipping').html(data);
            },
            error: function(e) {
                //called when there is an error
                console.log(e.message);
            }
        });
    });
});
</script>

因此,对于上面的所有代码,看来我已经接近我正在寻找的内容,但需要一些额外的指导。

对此的任何帮助将不胜感激。

谢谢,

jAC

【问题讨论】:

  • 你的ajax在哪里?我们需要更多信息来帮助解决这个问题。或者你不是真的指ajax,而只是jQuery? (因为值在您的输入元素中)?
  • @cale_b 我​​现在没有任何工作。这就是我需要帮助的。我不知道它应该是ajax还是jquery。两者对我来说都很新。
  • @cale_b 添加了我尝试过的脚本(一个 jquery 和一个 ajax)
  • 好的。建议编辑问题并删除 AJAX 标记,并对其进行更改,使其不显示“...将其添加到 PHP 变量...”。另外,请在下面查看我的答案 - 如果它不正确/不正确,请告诉我(如何)以便我可以修复它。
  • @cale_b 将它添加到 php 变量正是我需要的,这就是为什么我认为我可能需要 ajax

标签: php jquery html ajax


【解决方案1】:

如果我正确理解你的问题,你就不需要 AJAX。

首先,像这样修改你的 HTML:

    <tr>
        <td><?= $detail['type'] ?></td>
        <td><?= $detail['size'] ?></td>
        <td class="product_price">$<?= $detail['price'] ?>.00</td>
        <td>
            <input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="30">Express Shipping
            <input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="15">Regular Shipping
            <input type="radio" name="shipping_<?= $detail['id'] ?>" class="radio" value="0">Free Shipping
            <input type="hidden" class="shipping_amount" name="shipping_amount_<?php echo $detail['id']" value="">
        </td>
        <?php // Create a "container" for the price ?>
        <td class="shipping"></td>
    </tr>

然后,在 &lt;head&gt; 中的输出/顶部,添加以下代码:

// Document Ready - wait until page is loaded
jQuery(function($) {
    // Select all inputs with name starting with "shipping".  Don't need script inside of the loop
    $(document).on('click', 'input[name^="shipping"]',
        function() {
            // Get the price from the clicked input
            var shipping = parseFloat($(this).attr('value'));
            var price = parseFloat($(this).html());
            var total = price + shipping;
            // Find the proper cell, and set the display to the amount
            $(this).closest('tr').find('td.shipping').html(shipping);
            $(this).closest('tr').find('input.shipping_amount').val(shipping);
            $(this).closest('tr').find('input[name^="totalCost"]').val(total);
        }
    );
});

这依赖于每个“运输”在表格行中,并且运输显示在表格单元格中在该行中

注意
上面的代码已被编辑为还支持在提交表单时传递的输入。由于该输入不在原始问题代码中,因此我做出了假设 - 查找“shipping_amount”。

附加说明 上面的代码已经过修改,还提供了对表单中“totalCost”输入的更新。
请注意一个类已添加到“price”单元格(td 元素)中。

如果您以这种方式提交此表单,您还需要:

  1. 提交要订购的产品 ID。这样在表单验证(PHP 端)上,您可以从数据库加载产品并验证价格。
  2. 提交正在选择的运输方式。这样您就可以验证运费。

为了做到这一点,您的表单标签可能应该包裹整个表格,并且您应该添加一个包含“产品 ID”的隐藏输入,以便您可以获取该信息以及选定的单选按钮。

祝你好运!

【讨论】:

  • 这接近我想要的,但我仍然需要返回分配给 php 变量的值,这就是我认为我需要 ajax 的原因。我必须能够获取该结果并将其传递给稍后生成的 pay pal 按钮。另请注意,我的输入名称附加了一个 ID,因为 for 循环中可能有一组以上的单选按钮。它也可能并不总是一张桌子。我读的一些东西是让它使用一个类而不是输入类型,但我收到了太多相互冲突的消息。
  • 是的!我相信这正是我需要的!巧妙地设置输入隐藏字段以使其通过。非常感谢!我将编辑以删除 ajax 的东西。
  • Hrm... 几乎可以满足我的要求。我需要在商品离开页面之前将运费添加到商品价格中。
  • 所以,作为旁注 - 如果原始问题在本规范中很清楚,那将是非常棒。如果您需要特定代码来实现这一点,请告诉我。否则,我相信上面的所有难题都已为您准备好了。 (再次 - 让我知道,如果需要,我会很乐意让答案更加可靠)
  • 好的,让我用一个更准确的例子来改写这个问题(我一直在玩你给我的东西和其他一些 ajax 的东西)
【解决方案2】:

cale_b 是对的。看起来您在这里不需要 AJAX。当您需要与服务器通信时使用 AJAX,通常用于数据库功能(动态查找内容、存储新值等)。

AJAX 的意义在于它与服务器通信,向服务器发送数据并(可选)从服务器接收数据,无需刷新页面。 HTML 表单也会向服务器发送数据,但是一旦用户单击Submit,当前页面就会变为“操作”页面。

在您的示例中,您似乎需要使用 javascript 变量,而不一定是 PHP 变量。如果您只想从页面上获取信息,将其存储在一个变量中,(可能使用其他字段中的其他值执行一些数学运算),然后将产品吐回同一页面 - 不需要服务器交互。 Javascript/jQuery 可以做到这一点。

为了更好地了解 AJAX 的全部内容,这篇文章提供了一些很棒的初学者示例:

AJAX request callback using jQuery

这是一个很好的(free) introduction video to AJAX

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-23
    • 1970-01-01
    • 2015-06-26
    相关资源
    最近更新 更多