从后面的代码调用jquery函数不起作用

时间:2014-09-18 02:08:26

标签: c# jquery asp.net code-behind buttonclick

我在一个单独的.js文件中有一个名为MyFunc()的jquery函数。我想在按钮点击事件后面的c#代码中调用此函数。 (即如果成功添加数据,请致电MyFunc())。 最初这个功能看起来像这个

$(".next").click(function () {

在点击asp:按钮期间直接调用它。

<asp:button id="btnNext" runat="server" CssClass="next action-button" Text="Next" OnClientClick="return false"/>

所以我将功能改为

function MyFunc(){}

和按钮

<asp:Button ID="btnNext" runat="server" Text="Next" CssClass="action-button" OnClick="btnNext_Click1"/>

并在按钮点击事件后面的代码中

Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyScript", "Easying.js");
Page.ClientScript.RegisterClientScriptInclude(GetType(), "MyScript", "Reg.js");
Page.ClientScript.RegisterStartupScript(GetType(), "MyScript", "MyFunc()", true);

它没有给我任何错误。但它不起作用。 以下是功能。

var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

$(".next").click(function() {

    if (animating) return false;
    animating = true;

    current_fs = $(this).parent();
    next_fs = $(this).parent().next();

    //activate next step on progressbar using the index of next_fs
    $("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

    //show the next fieldset
    next_fs.show();
    //hide the current fieldset with style
    current_fs.animate({ opacity: 0 }, {
        step: function (now, mx) {
            //as the opacity of current_fs reduces to 0 - stored in "now"
            //1. scale current_fs down to 80%
            scale = 1 - (1 - now) * 0.2;
            //2. bring next_fs from the right(50%)
            left = (now * 50) + "%";
            //3. increase opacity of next_fs to 1 as it moves in
            opacity = 1 - now;
            current_fs.css({ 'transform': 'scale(' + scale + ')' });
            next_fs.css({ 'left': left, 'opacity': opacity });
        },
        duration: 800,
        complete: function () {
            current_fs.hide();
            animating = false;
        },
        //this comes from the custom easing plugin
        easing: 'easeInOutBack'
    });
});

另外,正如您所看到的,MyFunc()用于从一个字段集更改为另一个字段集。我的aspx页面有3个字段集。和进度条(1--2--3)。进度条应该从一个移动到另一个。而且该字段集也在改变。目前的情况是我看到进度条从1移动到直接3.并且在fieldset中没有变化。 这是我为它采取代码的地方。正如我上面提到的,当我直接从aspx页面调用它时,它工作得很好。 http://codepen.io/atakan/pen/gqbIz

继承人MyFunc()!!

function MyFunc() {
var current_fs, next_fs, previous_fs; //fieldsets
var left, opacity, scale; //fieldset properties which we will animate
var animating; //flag to prevent quick multi-click glitches

if (animating) return false;
animating = true;

current_fs = $(this).parent();
next_fs = $(this).parent().next();

//activate next step on progressbar using the index of next_fs
$("#progressbar li").eq($("fieldset").index(next_fs)).addClass("active");

//show the next fieldset
next_fs.show();
//hide the current fieldset with style
current_fs.animate({ opacity: 0 }, {
    step: function (now, mx) {
        //as the opacity of current_fs reduces to 0 - stored in "now"
        //1. scale current_fs down to 80%
        scale = 1 - (1 - now) * 0.2;
        //2. bring next_fs from the right(50%)
        left = (now * 50) + "%";
        //3. increase opacity of next_fs to 1 as it moves in
        opacity = 1 - now;
        current_fs.css({ 'transform': 'scale(' + scale + ')' });
        next_fs.css({ 'left': left, 'opacity': opacity });
    },
    duration: 800,
    complete: function () {
        current_fs.hide();
        animating = false;
    },
    //this comes from the custom easing plugin
    easing: 'easeInOutBack'
});};

2 个答案:

答案 0 :(得分:0)

由于你的代码在这里不起作用

current_fs = $(this).parent();
next_fs = $(this).parent().next();
$(this)中的{p> MyFunc()不是像您使用$(".next").click(function() { ... }时那样被点击的按钮,因此您的代码无法正常工作,就像您使用{{1}时一样使用click的函数,现在你可以使用jQuery中的参数为当前和下一个项目发送class来克服它,所以你可以这样写它

MyFunc()

它尚未经过测试,但您应该可以使用此逻辑进行测试。

答案 1 :(得分:0)

您的问题是您正在混淆变量范围。你的功能MyFunc()试图设置current_fs和next_fs:

current_fs = $(this).parent();
next_fs = $(this).parent().next();

但是这个&#39;这个&#39;关键字引用将在从单击事件调用函数的上下文中使用时更改,它将引用已单击的按钮,而不是通过Page从代码隐藏方法调用它时。 ClientScript.RegisterStartupScript()方法,它将引用一些可能对您没用的其他对象。

因此,您必须使用不同的方法使函数正确识别当前字段集和下一个字段集。

我的建议是完全改变你的实现(如果可能的话),以便最终的提交按钮是唯一的回发动作,因为它是一个更简单的解决方案,不仅可以带来更流畅的用户体验,还可以减少之间的流量客户端和服务器。

相关问题