从a href onclick加载jQuery函数
我有一个HTML表格,每行有一列,在从a href onclick加载jQuery函数,jquery,onclick,modal-dialog,partial-views,Jquery,Onclick,Modal Dialog,Partial Views,我有一个HTML表格,每行有一列,在标记中有此HTML代码: <a onclick="modalDialog(this); return false;" href="javascript:void(0)">17795</a> <div style="display: none;" class="MessageDataDiv"> some text to show up in my jquery modal window when funct
标记中有此HTML代码:
<a onclick="modalDialog(this); return false;" href="javascript:void(0)">17795</a>
<div style="display: none;" class="MessageDataDiv">
some text to show up in my jquery modal window when function modaldialog is clicked.
</div>
我如何编写jQuery函数,使函数在单击链接时只触发一次?我需要将jQuery函数modalDialog作为一个单独的函数,因为html表使用AJAX部分呈现
问题是,当我第一次单击a href链接时,什么都没有发生,第二次单击链接时,我会看到两个模态对话框,第三次单击时,我会看到三个模态对话框,依此类推 你可以用这样的东西 首次入境时:
function modalDialog(event) {
if (!$(document).data("clicked"))
{
$(document).data("clicked", true);
$('a.message').click(function () {
var div = new $(this).closest("td").find(".MessageDataDiv").clone();
div.show().dialog();
event.preventDefault();
});
}
}
您还可以使用“return false;”而不是触发event.preventDefault() 你可以用这样的东西 首次入境时:
function modalDialog(event) {
if (!$(document).data("clicked"))
{
$(document).data("clicked", true);
$('a.message').click(function () {
var div = new $(this).closest("td").find(".MessageDataDiv").clone();
div.show().dialog();
event.preventDefault();
});
}
}
您还可以使用“return false;”而不是触发event.preventDefault() 我会用不同的方式来做。我将从这样的标记开始:
<div class="message">
<a href="#">17795</a>
<div>some text to show up in my jquery modal window when function modaldialog is clicked.</div>
</div>
和JS:
$("div.message > a").one("click", function() {
$(this).next().dialog();
return false;
});
我会用不同的方式来做。我将从这样的标记开始:
<div class="message">
<a href="#">17795</a>
<div>some text to show up in my jquery modal window when function modaldialog is clicked.</div>
</div>
和JS:
$("div.message > a").one("click", function() {
$(this).next().dialog();
return false;
});
我认为最简单的方法是使用一个div来显示其他文本
<div id="MessageDataDiv"></div>
<script>
$(function() {
$('#MessageDataDiv').dialog({ autoOpen: false });
});
function modalDialog(a) {
if($(a).data('clicked')) return false;
$(a).data('clicked', true);
$('#MessageDataDiv').html($(a).next().text()).dialog();
return false;
}
</script>
$(函数(){
$('#MessageDataDiv')。对话框({autoOpen:false});
});
函数modalDialog(a){
if($(a).data('clicked'))返回false;
$(a).数据('点击',为真);
$('#MessageDataDiv').html($(a).next().text()).dialog();
返回false;
}
这样,您就不必在每次打开对话框时都创建一个div。我认为最简单的方法是使用一个div来显示其他文本
<div id="MessageDataDiv"></div>
<script>
$(function() {
$('#MessageDataDiv').dialog({ autoOpen: false });
});
function modalDialog(a) {
if($(a).data('clicked')) return false;
$(a).data('clicked', true);
$('#MessageDataDiv').html($(a).next().text()).dialog();
return false;
}
</script>
$(函数(){
$('#MessageDataDiv')。对话框({autoOpen:false});
});
函数modalDialog(a){
if($(a).data('clicked'))返回false;
$(a).数据('点击',为真);
$('#MessageDataDiv').html($(a).next().text()).dialog();
返回false;
}
这样,您就不必每次打开对话框时都创建一个函数。如果要这样做,最好使用jQuery.one(),而不是绑定/取消绑定。Docs:如果要这样做,最好使用jQuery.one(),而不是绑定/取消绑定。Docs:这是一种正确而干净的方法,可以用它重写我的代码。它很有魅力。谢谢。这是一个正确和干净的方法来重写我的代码。它很有魅力。非常感谢。