doujia9833 2019-04-05 10:46
浏览 50

ajax在第一次点击时没有工作,经过2次点击后多次运行

i have a table that its data builds by ajax and afterward on each checkbox click another ajax should run .to get click event on checkbox i take it to a change event and the problem is that first click not work and after 2 times checked checkbox it runs two time.

$('.subterms >tbody').on('change',function () {
        $('.sub').on('click','input',function () {
            let parent = $(this).val();
            if ($(this).is(':checked')){
                $.ajax({
                    url: ajaxurl,
                    type: "POST",
                    data: {
                        'action': 'getChild',
                        'dataType': 'json',
                        'parent': parent
                    },
                    success: function (data) {
                        console.log(data);
                        $(".subterms >tbody").append(data);
                    }
                });
            }
            else{
                let parent = $(this).val();
                $('tr[data-id="' + parent + '"]').remove();
            }
        });
    });

the ajax return true values but ajax run more than one time and each time run double Previous time.
EDITE:
this is my html code.

<table class="form-table subterms">
     <tbody>
        <tr class="sub" data-id="61">
            <td><span>festival</span></td>
            <td>
               <label>
                 <input type="checkbox" name="person-referee[]" value="80">
                 <span>one</span>
               </label>
               <label>
                 <input type="checkbox" name="person-referee[]" value="77">
                 <span>two</span>
               </label>
               <label>
                 <input type="checkbox" name="person-referee[]" value="79">
                 <span>three</span>
               </label>
               <label>
                 <input type="checkbox" name="person-referee[]" value="78">
                 <span>four</span>
               </label>
               <label>
                 <input type="checkbox" name="person-referee[]" value="82">
                 <span>five</span>
               </label>
            </td>
        </tr>
   </tbody>
</table>

after ajax run a <tr class='sub'> will be added to the table.

  • 写回答

1条回答 默认 最新

  • dsfsw1233 2019-04-05 10:49
    关注

    If you follow the code flow:

    onChange of .subterms >tbody you will append a new click listener to .sub ... on every change!

    So each time the element changes, there will be a new click handler. So when you click on .sub, every one of those handlers will be executed.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分