gpt4 book ai didi

javascript - 使用 jQuery 时如何在 ASP.NET 中同时触发 OnClick 和 OnClientClick 事件?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:37:10 25 4
gpt4 key购买 nike

我试图在回发期间显示消息栏。我正在使用我在网上找到的 jQuery 脚本 here .一切都很好,但是当 jQuery 添加到页面时,我的服务器端事件永远不会被调用。

这是我的按钮代码:

<asp:Button ID="btnGetReport" CssClass="float-left" runat="server" 
Text="<%$ Glossary:GetReport %>" OnClick="btnGetReport_Click" />

这是内联脚本:

<script type="text/javascript">
$(document).ready(function () {
$('#<%=btnGetReport.ClientID%>').click(function (event) {
event.preventDefault();
$('#message_bar').displayMessage({
position: 'fixed',
skin: 'modern',
message: 'We are fetching your report. Please wait...',
});
});
});
</script>

这是相关的外部 .js 文件:

(function( $ ){

$.fn.displayMessage = function(options) {

// Default configuration properties.
var defaults = {
message : 'Error message',
speed : 'fast',
position : 'fixed', // relative, absolute, fixed
autohide : true
}

var options = $.extend( defaults, options );
$(this).slideUp('fast');
$(this).removeClass().empty();
return this.each(function() {

var sticky = (options.sticky == false) ? 'relative' : 'absolute';
$(this).addClass('messagebar-skin-'+options.skin+'_bar').css('position',options.position).css('background-color',options.background);
$(this).append('<div class="messagebar-skin-'+options.skin+'_inner"><span class="messagebar-skin-'+options.skin+'_text"></span></div>').css('color',options.color);
$(this).find('span').html(options.message);

$(this).slideDown(options.speed ,function(){

var parent = ($(this).attr('id')) ? "#"+$(this).attr('id') : "."+$(this).attr('class');
var close_button = ($(this).find('a').attr('id')) ? "#"+$(this).find('a').attr('id') : "."+$(this).find('a').attr('class');

if(options.autohide == true)
{
$(this).delay(2400).fadeOut('slow');
}

$(parent+">div>"+close_button).bind("click", function (event) {
event.preventDefault();
$(parent+">div>"+close_button).animate({"opacity": "hide"}, function(){
$(parent+">div>span").fadeOut("slow").html("");
$(parent+">div>"+close_button).parent().parent().slideUp(options.speed);
});
});

});

});

};
})( jQuery );

我也尝试过使用 OnClientClick 属性调用该函数,但似乎都不起作用。我查看了其他示例,其中人们对此有疑问,但似乎都没有帮助。

如有任何想法,我们将不胜感激!

最佳答案

你有没有尝试过一些简单的事情,比如:

OnClientClick="return YourJavaScriptFunction();"

<asp:Button ID="btnGetReport" CssClass="float-left" runat="server"
Text="<%$ Glossary:GetReport %>" OnClick="btnGetReport_Click"
OnClientClick="return YourJavaScriptFunction();" />

在你的 JavaScript 函数中,最后返回 true

function YourJavaScriptFunction () {
// your cool stuff
return true;
}

编辑1

这一行:

OnClientClick="return YourJavaScriptFunction();"

相当于:

$('#<%=btnGetReport.ClientID%>').click(function () {

你的脚本看起来像:

<script type="text/javascript">

function YourJavaScriptFunction (){
$('#message_bar').displayMessage({
position: 'fixed',
skin: 'modern',
message: 'We are fetching your report. Please wait...'
});

// this will prevent the postback, equivalent to: event.preventDefault();
return false;
}

</script>

关于javascript - 使用 jQuery 时如何在 ASP.NET 中同时触发 OnClick 和 OnClientClick 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11453985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com