dongmie3526 2018-12-19 19:08
浏览 44

有什么我可以做而不是$(“输入:无线​​电”)。更改(function(){}以在单击某个无线电输入时激活AJAX?

I have this loop that loops through my database table tvshows to get the names of the T.V shows and output them to the screen with a radio input:

<div style="width: 200px;">

  <div>
    <?php

        $query = "SELECT distinct(title), (id) from tvshows";
        $featured = mysqli_query($conn,$query);

            while ($row = mysqli_fetch_assoc($featured)) { ?>

                 <div class="list-group-item checkbox" style="  background- 
                 color: #f4f4f4;">

                    <label ><input type="radio" name="re" 
                    class="common_selector collection" style="" value="<?php 
                    echo $row['title']; ?>" id='sale'>   <?php echo 
                    $row['title']; ?></label>
                </div>
                <?php
                }
                ?>
   </div>
</div> 

Here is the AJAX:

 $("input:radio").change(function() {
            var product_id = $(this).attr('id');    
            var coll = $(this).attr('value'); 
            var action = "check";

      if ($(this).is(":checked")) {
            $.ajax({
                url:"../PHP_Scripts/fetch_data.php",
                method: "POST",
                dataType: "json",
                data:{
                    coll:coll, action:action, product_id:product_id
                },
                success:function(data){
                    $('#product_wrapper').html(data);
                    alert("success");

                }
          });
 });

It's the $("input:radio").change(function() {} that is my issue. To summarise why this problem is happening: the script PHP_Scripts/fetch_data.php activates every time the radio input is changed. Its function is to output the products based off of which radio button is clicked. It's an issue because the PHP_Scripts/fetch_data.php also contains some AJAX that adds the product to the cart, so when the radio input changes, and you try to add a product to the cart, the product gets added to the cart the same amount of times as the input has been changed. So if you check a radio input 3 times, then click the button that adds a product to the cart, it will be added 3 times.

The reason I didn't paste every page is that the issue just one line: $("input:radio").change(function() {}. Does anyone know an alternative I can use to activate the AJAX when say, a certain input is checked?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch