如何通过从ajax文件生成的按钮调用jquery单击函数

hi i have a form which appends a table when a user inputs a value for eg. if a user inputs '1' then the values related to '1' will be appended on a table and the table will be appended on my page but i am even calling a function of jquery which will append a table row when add button is pressed but the function is not working is it because i am generating the button by ajax???? can anyone help me in this

here is my script to generate a new row

  $(document).ready(function() {
          var currentItem = $('#items').val();
                $('#addnew').click(function() {
          var strToAdd = '<tr><td align="center"><input type="text" size="6" maxlength="6" id="ord_' + currentItem + '" maxlength="6" name="ord_' + currentItem + '" class="form-input-oth" onkeyup="return copy(this.val());"/></td>
\
                                        <td align="center"><input type="text" size="6" maxlength="6" id="srno' + currentItem + '" maxlength="6" name="srno_' + currentItem + '" class="form-input-oth"/></td>
\
                                        <td align="center"><textarea name="descrip_' + currentItem + '" id="descrip_' + currentItem + '" cols="70" class="form-input-textarea"></textarea></td>
\
                                        <td align="center"><input type="text" size="6" maxlength="9" id="unit_' + currentItem + '" maxlength="6" name="unit_' + currentItem + '" class="form-input-rate"/></td>
\
                                        <td align="center"><input type="text" size="6" maxlength="9" id="rate_' + currentItem + '" maxlength="6" name="rate_' + currentItem + '" class="form-input-rate"/></td></tr>';
                     $('#data').append(strToAdd);
                       currentItem++;
                       $('#items').val(currentItem);

                });
            });

here is my textbox from which i am calling the ajax function

<input type="text" size="6" maxlength="6" id="code" maxlength="6" name="quotno" value="<?php echo ($addflag == 0) ? $get['quotno'] : ""; ?>" class="form-input-code"  onblur="showyear('boqsrch.php?code='+this.value);"/>

here is my file from which the table is generated

<?php
require("includes/dbconnect.php");
include ('includes/function.php');

$code = trim($_GET["code"]);

$result = mysql_query("SELECT * FROM quotdtl WHERE vouchno='$code'") or die(mysql_error());

if (mysql_num_rows($result) > 0) {
    echo '<div style="margin-left: 400px; margin-right: auto;  width:180px;">
            <h2><font color="#0099FF"  face="Times New Roman">Bill Of Quotation</font></h2>
          </div>
          <hr/>
          ';
    echo "<table border=\"1px\" width=\"80%\" id=\"data\" align=\"center\">";
;?>

                  <tr>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Order</font><span></span></label></td>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Sr No.</font><span></span></label></td>
                     <td width="580px" align="center"><label for=""><font color="#0099FF" size="3px">Description</font><span></span></label></td>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Unit</font><span></span></label></td>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Rate</font><span></span></label></td>
                  </tr>

<?php

    $ctr = 0;
    while ($row = mysql_fetch_array($result)) {
        echo "<tr>";
        echo "<td align=\"center\"><input type=\"text\" size=\"6\" maxlength=\"6\" id=\"ord_$ctr\" maxlength=\"6\" name=\"ord_$ctr\" value=\"$row[orderby]\" class=\"form-input-oth\" onkeyup=\"document.getElementById('ord_$ctr').value = this.value;\"/></td>";
        echo "<td align=\"center\"><input type=\"h\" size=\"6\" maxlength=\"6\" id=\"srno_$ctr\" maxlength=\"6\" name=\"srno_$ctr\" value=\"$row[srno]\" class=\"form-input-oth\" onkeyup=\"document.getElementById('srno_$ctr').value = this.value;\"/></td>";
        echo "<td align=\"center\"><textarea name=\"descrip_$ctr\" cols=\"70\" class=\"form-input-textarea\" onkeyup=\"document.getElementById('descrip_$ctr').value = this.value;\">$row[descrip]</textarea></td>";
        echo "<td align=\"center\"><input type=\"text\" size=\"6\" maxlength=\"9\" id=\"unit_$ctr\" maxlength=\"6\" name=\"unit_$ctr\" value=\"$row[unit]\" class=\"form-input-rate\" onkeyup=\"document.getElementById('unit_$ctr').value = this.value;\"/></td> ";
        echo "<td align=\"center\"><input type=\"text\" size=\"6\" maxlength=\"9\" id=\"rate_$ctr\" maxlength=\"6\" name=\"rate_$ctr\" value=\"$row[rate]\" class=\"form-input-rate\" onkeyup=\"document.getElementById('rate_$ctr').value = this.value;\"/></td>";
        echo "</tr>";
        $ctr++;
    }
    echo '</table>';

} else {
    ?>
            <div style="margin-left: 400px; margin-right: auto;  width:180px;">
                <h2><font color="#0099FF"  face="Times New Roman">Bill Of Quotation</font></h2>
            </div>
            <hr/>
                 <table border="1px" width="80%" id="data" align="center">
                  <tr>
                      <td><br /></td>
                  </tr>

                  <tr>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Order</font><span></span></label></td>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Sr No.</font><span></span></label></td>
                     <td width="580px" align="center"><label for=""><font color="#0099FF" size="3px">Description</font><span></span></label></td>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Unit</font><span></span></label></td>
                     <td width="130px" align="center"><label for=""><font color="#0099FF" size="3px">Rate</font><span></span></label></td>
                  </tr>
                  <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="ord_0" maxlength="6" name="ord_0" value="" class="form-input-oth" onkeyup="document.getElementById('ord_0').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="srno_0" maxlength="6" name="srno_0" value="" class="form-input-oth" onkeyup="document.getElementById('srno_0').value = this.value;"  /></td> 
                    <td align="center"><textarea name="descrip_0" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_0').value = this.value;"></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="unit_0" maxlength="6" name="unit_0" value="" class="form-input-rate" onkeyup="document.getElementById('unit_0').value = this.value;"  /></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="rate_0" maxlength="6" name="rate_0" value="" class="form-input-rate" onkeyup="document.getElementById('rate_0').value = this.value;"  /></td> 
                  </tr>
                  <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="ord_1" value="" class="form-input-oth" onkeyup="document.getElementById('ord_1').value = this.value;"  /></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="srno_1" value="" class="form-input-oth" onkeyup="document.getElementById('srno_1').value = this.value;"  /></td> 
                    <td align="center"><textarea name="descrip_1" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_1').value = this.value;"></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="unit_1" value="" class="form-input-rate" onkeyup="document.getElementById('unit_1').value = this.value;"  /></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="rate_1" value="" class="form-input-rate" onkeyup="document.getElementById('rate_1').value = this.value;"  /></td> 
                  </tr>
                   <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="ord_2" value="" class="form-input-oth" onkeyup="document.getElementById('ord_2').value = this.value;"  /></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="srno_2" value="" class="form-input-oth" onkeyup="document.getElementById('srno_2').value = this.value;"  /></td> 
                    <td align="center"><textarea name="descrip_2" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_2').value = this.value;"/></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="unit_2" value="" class="form-input-rate" onkeyup="document.getElementById('unit_2').value = this.value;"  /></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="rate_2" value="" class="form-input-rate" onkeyup="document.getElementById('rate_2').value = this.value;" /></td> 
                  </tr>
                  <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="ord_3" value="" class="form-input-oth" onkeyup="document.getElementById('ord_3').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="srno_3" value="" class="form-input-oth" onkeyup="document.getElementById('srno_3').value = this.value;" /></td> 
                    <td align="center"><textarea name="descrip_3" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_3').value = this.value;"></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="unit_3" value="" class="form-input-rate" onkeyup="document.getElementById('unit_3').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="rate_3" value="" class="form-input-rate" onkeyup="document.getElementById('rate_3').value = this.value;"/></td> 
                  </tr>
                  <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="ord_4" value="" class="form-input-oth"  onkeyup="document.getElementById('ord_4').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="srno_4" value="" class="form-input-oth"  onkeyup="document.getElementById('srno_4').value = this.value;" /></td> 
                    <td align="center"><textarea name="descrip_4" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_4').value = this.value;"></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="unit_4" value="" class="form-input-rate" onkeyup="document.getElementById('unit_4').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="rate_4" value="" class="form-input-rate" onkeyup="document.getElementById('rate_4').value = this.value;"  /></td> 
                  </tr>
                  <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="ord_5" value="" class="form-input-oth" onkeyup="document.getElementById('ord_5').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="srno_5" value="" class="form-input-oth" onkeyup="document.getElementById('srno_5').value = this.value;"  /></td> 
                    <td align="center"><textarea name="descrip_5" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_5').value = this.value;"></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="unit_5" value="" class="form-input-rate" onkeyup="document.getElementById('unit_5').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="rate_5" value="" class="form-input-rate" onkeyup="document.getElementById('rate_5').value = this.value;"/></td> 
                  </tr>
                  <tr>
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="ord_6" value="" class="form-input-oth" onkeyup="document.getElementById('ord_6').value = this.value;"/></td> 
                    <td align="center"><input type="text" size="6" maxlength="6" id="code" maxlength="6" name="srno_6" value="" class="form-input-oth" onkeyup="document.getElementById('srno_6').value = this.value;"/></td> 
                    <td align="center"><textarea name="descrip_6" cols="20" class="form-input-textarea" onkeyup="document.getElementById('descrip_6').value = this.value;"></textarea></td>
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="unit_6" value="" class="form-input-rate"  onkeyup="document.getElementById('unit_6').value = this.value;" /></td> 
                    <td align="center"><input type="text" size="6" maxlength="9" id="code" maxlength="6" name="rate_6" value="" class="form-input-rate"  onkeyup="document.getElementById('rate_6').value = this.value;" /></td> 
                  </tr>
                  <?php }?>
            </table>

                         &nbsp;<input type="button" id="addnew" class="addnew" class="classname" name="addnew" value="+" /> 
                          <input type="hidden" id="items" name="items" value="7" />                 
doubu5035
doubu5035 但是$('body')。on('click','#addnew',(function(){});不起作用
大约 7 年之前 回复
duanmei1946
duanmei1946 听起来你需要.on(),只要你在jQuery1.7+上
大约 7 年之前 回复
doukaojie8573
doukaojie8573 你需要使用.ON()JQuery函数:$('body')。on('click','#addnew',(function(){});
大约 7 年之前 回复
douqiao5440
douqiao5440 是的我会,但你可以建议我解决我的问题吗?
大约 7 年之前 回复
dtx3006
dtx3006 不推荐使用MySQL(mysql_*functions)扩展。我建议使用MySQLi(mysqli_*函数)或PDO。您的代码也很容易受到SQL注入攻击。
大约 7 年之前 回复

2个回答



您应该使用以下内容:</ p>

  //新方式(jQuery 1.7+  ) -  .on(事件,选择器,处理程序)
$('#container')。on('click','#inputButtonID',function(event){
event.preventDefault();
alert(' testlink');
});
</ code> </ pre>

这会将您的事件附加到 #container </ code>元素中的任何输入,
,缩小 必须检查整个 document </ code>元素树并提高效率的范围。</ p>

此处有更多信息:</ p>

展开原文

原文

You should use the following:

// New way (jQuery 1.7+) - .on(events, selector, handler)
$('#container').on('click', '#inputButtonID', function(event) {
    event.preventDefault();
    alert('testlink'); 
});

This will attach your event to any input within the #container element, reducing the scope of having to check the whole document element tree and increasing efficiency.

More info here:

dongmiao260399
dongmiao260399 好的,只需创建一个新页面并粘贴<!DOCTYPE html> <html> <head> <style> p {color:red; } </ style> <script src =“http://code.jquery.com/jquery-1.9.1.js> </ script> <script> $(document).ready(function(){$(“ p“)。text(”DOM现在被加载并且可以被操纵。“);}); </ script> </ head> <body> <p>尚未加载。</ p> </ body> </ html>。 看看你在页面上得到了什么? 是DOM现在已经加载并且可以被操作。 红色???
大约 7 年之前 回复
doudui5753
doudui5753 很遗憾地说,但即使那不工作:(
大约 7 年之前 回复
duanqu9292
duanqu9292 是的,看起来你的脚本标签不正确。 注释掉并使用它来代替<script src =“http://code.jquery.com/jquery-1.9.1.js> </ script>。 一切都会好起来的!
大约 7 年之前 回复
dsuw85815
dsuw85815 包含的文件不正确
大约 7 年之前 回复
dongri1989
dongri1989 演示网站正在运行
大约 7 年之前 回复
douhan9748
douhan9748 我已经包含了这个文件 - > <script src =“code.jquery.com/jquery-latest.js”> </ script>
大约 7 年之前 回复
dongshi8038
dongshi8038 小提琴网站上的演示是否适合您? 您是否在代码中包含了所需的jQuery v1.9.1文件? 请检查!
大约 7 年之前 回复
duanlachu7344
duanlachu7344 先生,我试过你的演示中的代码,但即使那样也行不通
大约 7 年之前 回复
dongzong3053
dongzong3053 是的,即使我想,如果你能找到任何错误,那将是一个巨大的帮助
大约 7 年之前 回复
dongwei4096
dongwei4096 看这个演示。 这适用于动态创建的元素...
大约 7 年之前 回复
drmgg4411
drmgg4411 这应该工作! 我想,问题出在代码中的其他地方。
大约 7 年之前 回复
doumu9019
doumu9019 nopes它不工作
大约 7 年之前 回复
dqbh8054
dqbh8054 试试这个 - > $(document).on('click','#addnew',function(event){event.preventDefault(); alert('testlink');});
大约 7 年之前 回复
doutuo1908
doutuo1908 不,先生,我试过,即使那不工作
大约 7 年之前 回复



尝试使用</ code>上的绑定click事件,而不是 click </ code> http://api.jquery.com/on/ </ p>

这将允许 处理与选择器匹配的任何事件的行为,即使是在绑定发生之后发生的事件。</ p>
</ div>

展开原文

原文

Try binding the click event using on instead of click http://api.jquery.com/on/

This will allow the behavior to work on any event matching the selector even ones that came to be after the binding takes place.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问