dtrpv60860 2016-09-30 07:40
浏览 44

jQuery:Blur函数没有调用每个动态添加的行[重复]

This question already has an answer here:

I am newbie to jQuery.I am trying to call the Blur function on each dynamically Added row and send the values of each row of both text box to PHP.
For first row, it works but dynamically added rows, its not working.
My Code is as follows:
HTML and Jquery:

 <!doctype HTML>
    <html>
    <head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    </head>
    <script type="text/javascript">
    var rowCount = 1;
    function addMoreRows(frm) {
    rowCount ++;
    var recRow = '<p id="rowCount'+rowCount+'"><tr><td><input name="" type="text" id="co_name" size="17%"  maxlength="120" /></td><td><input name="" type="text"  id="co_qty" maxlength="120" style="margin: 4px 5px 0 5px;"/></td></tr> <a href="javascript:void(0);" onclick="removeRow('+rowCount+');">Delete</a></p>';
    jQuery('#addedRows').append(recRow);
    }

    function removeRow(removeNum) {
    jQuery('#rowCount'+removeNum).remove();
    }
    </script>
    <body>
    <table rules="all" style="background:#fff;">
    <tr>
    <td style="font-size:14px;" >Name</td>
    <td style="font-size:14px;" >Email</td>

    <td><span style="font:normal 12px agency, arial; color:blue; text-decoration:underline; cursor:pointer;" onclick="addMoreRows(this.form);">
    Add More
    </span>
    </td>
    </tr>
    <tr id="rowId">
    <td><input name="" type="text"  id="co_name" value="" size="17%"/></td>
    <td><input name="" type="text" id="co_qty" value="" /></td>

    </table>
    <div id="addedRows"></div>
    </td>
    </tr>
    <script>
    $(document).ready(function(){
        $('#co_qty').on('blur',function () {
    var username = $('#co_name').val(); 
     var password = $('#co_qty').val(); 
    $.ajax({
             type: "POST",
             url: "blur.php",
             data: {'title':username ,'title1':password},
             success: function(msg) {
      alert(msg)             }

    }); 
     });
    });

    </script>
    </body>
    </html><br>

blur.php

$var=$_POST['title'];
$var1=$_POST['title1'];
echo $var."Success!".$var1;


I am not getting it. Please do suggest me.Thanks in advance.

</div>
  • 写回答

1条回答 默认 最新

  • douxiaqin2062 2016-09-30 07:41
    关注

    Use on() to bind dynamically added elements on DOM.

    Because when DOM is ready, it doesn't have those elements and when you add those dynamically, DOM doesn't understand those.

    $('body').on('blur','.co_qty', function () {
       //Your code
    });
    

    And I can see that you've multiple elements (text-box) with same id #co_qty, it cause problems.

    You need to use .co_qty to apply on dynamically added elements.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看