无法在ASP.Net按钮上单击javascript函数. [英] Not able to call javascript Function on ASP.Net Button Click..

查看:59
本文介绍了无法在ASP.Net按钮上单击javascript函数.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个用于实现计时器的JavaScript.

 功能 CountUp(initDate,id){
             .beginDate =  日期(initDate );
             .countainer = 文档 .getElementById(id);
             .numOfDays = [ 31  28  31  30  31  30  31  31  30  31  30  .borrowed =  0 .年=  0 .months=  0  this  .days =  0 ;
             .hours =  0  this .分钟=  0  this .seconds=  0 ;
             .updateNumOfDays();
             .updateCounter();
        }

        CountUp.prototype.updateNumOfDays = 功能(){
             var  dateNow =  日期();
             var  currYear = dateNow.getFullYear();
            如果((currYear% 4  ==  0 & currYear% 100 !=  0 )|| currYear% 0 ){
                 .numOfDays [ 1 ] =  29  ;
            }
             var  self = ;
            setTimeout(函数(){self.updateNumOfDays();},(  1 ), 2 )-dateNow));
        }

        CountUp.prototype.datePartDiff = 函数(然后,现在是MAX){
             var  diff =现在-然后-.
             this  .borrowed =  0 ;
            如果(diff> -1) return  diff;
             this  .borrowed =  1 ;
            返回(最大+差异);
        }

        CountUp.prototype.calculate = 函数(){
             var  currDate =  日期();
             var  prevDate =  this  .beginDate;
             .seconds =  .datePartDiff(prevDate.getSeconds(),currDate.getSeconds(), 60 );
             .minutes =  .datePartDiff(prevDate.getMinutes(),currDate.getMinutes(), 60 );
             .hours =  .datePartDiff(prevDate.getHours(),currDate.getHours(), 24 );
             .days =  .datePartDiff(prevDate.getDate(),currDate.getDate(), .numOfDays [currDate.getMonth()]);
             .months =  .datePartDiff(prevDate.getMonth(),currDate.getMonth(), 12 );
             .years =  .datePartDiff(prevDate.getFullYear(),currDate.getFullYear(), 0 );
        }

        CountUp.prototype.addLeadingZero = 函数(值){
            返回值<  10 ? ("  + value):值;
        }

        CountUp.prototype.formatTime = 函数(){
             this  .seconds =  this  .addLeadingZero( this  .seconds);
             this  .minutes =  this  .addLeadingZero( this .分钟);
             this  .hours =  this  .addLeadingZero( this  .hours);
        }

        CountUp.prototype.updateCounter = 函数(){
             .calculate();
             .formatTime();
             .countainer.innerHTML = "  +
        "  + .小时+ "  +( 1 ? :" )+  </small>" +
        "  + .分钟+ "  +( 1 ? :" )+  </small>" +
        "  + .秒+ "  +( 1 ? :" )+  </small>";
             var  self = ;
            setTimeout(函数(){self.updateCounter();}, 1000 );
        }

        今天 var  =  日期();
        窗口 .onload = 功能(){ CountUp(今天,' 计数器'); }
 



它工作正常,但是正如我们看到的那样,由于我在页面加载即Window.Onload上调用函数时从页面加载启用了计时器,此处的"counter"是调用该方法的用户,该方法基本上是div的id .

我想在按钮单击事件上调用此函数,但我曾尝试过但未成功.请指导我如何在按钮单击事件上调用此方法,并且必须将系统当前日期传递给此函数,这也是强制性的. div class ="h2_lin">解决方案

您可以使用按钮的onClentClick属性

跟随链接
http://www.w3schools.com/aspnet/prop_webcontrol_button_onclientclick.asp [ ^ ]


您可以尝试以下代码:

在页面加载中编写以下代码:

 myButton.Attributes.Add(" "  + System.DateTime.Now + " ')"); 



希望对您有所帮助.


您可以尝试以下代码:

在页面加载中编写以下代码:

 myButton.Attributes.Add(" "  + System.DateTime.Now + " ')"); 



在js中创建一个新函数,它将像这样调用您创建的函数:-

函数NewFunction(dt)
{
CountUp(dt,' 计数器');
} 



并在js中删除window.load行


I have this javascript which i m using for implementing timer.

function CountUp(initDate, id) {
            this.beginDate = new Date(initDate);
            this.countainer = document.getElementById(id);
            this.numOfDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            this.borrowed = 0, this.years = 0, this.months = 0, this.days = 0;
            this.hours = 0, this.minutes = 0, this.seconds = 0;
            this.updateNumOfDays();
            this.updateCounter();
        }

        CountUp.prototype.updateNumOfDays = function () {
            var dateNow = new Date();
            var currYear = dateNow.getFullYear();
            if ((currYear % 4 == 0 && currYear % 100 != 0) || currYear % 400 == 0) {
                this.numOfDays[1] = 29;
            }
            var self = this;
            setTimeout(function () { self.updateNumOfDays(); }, (new Date((currYear + 1), 1, 2) - dateNow));
        }

        CountUp.prototype.datePartDiff = function (then, now, MAX) {
            var diff = now - then - this.borrowed;
            this.borrowed = 0;
            if (diff > -1) return diff;
            this.borrowed = 1;
            return (MAX + diff);
        }

        CountUp.prototype.calculate = function () {
            var currDate = new Date();
            var prevDate = this.beginDate;
            this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60);
            this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60);
            this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24);
            this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]);
            this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12);
            this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(), 0);
        }

        CountUp.prototype.addLeadingZero = function (value) {
            return value < 10 ? ("0" + value) : value;
        }

        CountUp.prototype.formatTime = function () {
            this.seconds = this.addLeadingZero(this.seconds);
            this.minutes = this.addLeadingZero(this.minutes);
            this.hours = this.addLeadingZero(this.hours);
        }

        CountUp.prototype.updateCounter = function () {
            this.calculate();
            this.formatTime();
            this.countainer.innerHTML = ""  +
        " " + this.hours + " <small>" + (this.hours == 1 ? "hour" : "hours") + "</small>" +
        " " + this.minutes + " <small>" + (this.minutes == 1 ? "minute" : "minutes") + "</small>" +
        " " + this.seconds + " <small>" + (this.seconds == 1 ? "second" : "seconds") + "</small>";
            var self = this;
            setTimeout(function () { self.updateCounter(); }, 1000);
        }

        var today = new Date();
        window.onload = function () { new CountUp(today, 'counter'); }



It is working fine but as we can see the timer is getting enabled from the page load as i m calling the function on Page load i.e Window.Onload, here ''counter'' is user for calling the method which is basically id of a div.

I want to call this fuinction on a button click event and i had tried but didnt get sucess.Please guide me that how we will call this method on Button Click event and we have to pass the system current date to this function also mandatory.

解决方案

You can use onClentClick property of button

follow the links
http://www.w3schools.com/aspnet/prop_webcontrol_button_onclientclick.asp[^]


You can try the below Code :

Write the below Code in your Page Load :

myButton.Attributes.Add("onclick","yourFunction('" + System.DateTime.Now + "')");



Hope this helps.


You can try the below Code :

Write the below Code in your Page Load :

myButton.Attributes.Add("onclick","NewFunction('" + System.DateTime.Now + "')");



in js create a new function which will call your created function like that:-

 function NewFunction(dt)
{
CountUp(dt, 'counter'); 
}



and in js remove window.load line


这篇关于无法在ASP.Net按钮上单击javascript函数.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆