档案index.php
<script type="text/javascript" src="//<?php echo $_SERVER["SERVER_NAME"];?>/javascript/jquery-1.10.2.min.js" ></script>
<script type="text/javascript">
$(document).ready(function() {
$( document ).on( 'click', '#txt_my_invoices', function() {
$.post("_my_invoices.php", function(data_my_invoices) {
$('#span_info').html(data_my_invoices);
});
});
});
</script>
<span id="txt_my_invoices" style="cursor:pointer; border:solid 1px grey;">My invoices</span>
<span id="span_info"></span>
档案_my_invoices.php
<script type="text/javascript">
$(document).ready(function() {
$( document ).on( 'click', '.edit_invoice', function() {
alert( this.id + ' edit invoce' );
});
});
</script>
<button class="edit_invoice" id="1e" type="button">Click Me!</button>
如果打开/重新加载文件index.php
并点击id="txt_my_invoices"
,请参阅按钮Click Me!
。点击按钮,一次获得alert( this.id + ' edit invoce' );
。
但如果我再次单击id="txt_my_invoices"
并单击按钮,则会发出2次警报。然后单击id="txt_my_invoices"
并单击按钮,3次警报,依此类推。
如何只获得一个警报(执行)?
尝试使用.unbind()
和.off()
,但不行,因为无法执行其他功能。
答案 0 :(得分:1)
<script type="text/javascript">
$(document).ready(function() {
$( document ).on( 'click', '.edit_invoice', function() {
alert( this.id + ' edit invoce' );
});
});
</script>
这会在您的脚本中反复出现,这会导致您的页面上出现多个事件处理程序。
据我了解,您希望从外部脚本控制按钮及其行为。
您可以尝试类似下面的内容:
<script type="text/javascript">
$(document).ready(function() {
var handler = function() {
alert( this.id + ' edit invoce' );
};
$( ".edit_invoice" ).unbind( "click", handler);
$( ".edit_invoice" ).bind( "click", handler);
});
</script>