更新面板和 Javascript

发布于 2024-08-18 13:23:18 字数 802 浏览 2 评论 0原文

我有这样的内联脚本,它可以在编辑和显示 div 之间切换。如果它位于 UpdatePanel 内部,则需要单击两次才能工作。如果我删除 UpdatePanel,只需单击一下即可正常工作。

<a href="#" onclick="edit(event,'ctl00_CPH1_ctl00_win1')">Edit</a>

有人可以帮忙吗?

谢谢

编辑: 编辑功能:

function edit(e, id) {
    var editdiv = $('#' + id).find('.edit');
    var cntdiv = $('#' + id).find('.content');

    if (editdiv.css('visibility') == 'hidden') {
        editdiv.css('visibility') == 'visible'
        cntdiv.css('visibility') == 'hidden'
        cntdiv.hide();
        editbox.show()
    }
    else {
        editdiv.css('visibility') == 'hidden'
        cntdiv.css('visibility') == 'visible'
        cntbox.show();
        editbox.hide()
    }

    stopEventBubble(e); // Code to cancel event bubbling;
}

I have inline script such as this, which toggles between an edit and a display divs. If it is inside the the UpdatePanel it requires two click before it works. If I remove the UpdatePanel it works fine with a single click.

<a href="#" onclick="edit(event,'ctl00_CPH1_ctl00_win1')">Edit</a>

Can anyone help please?

Thanks

EDIT:
Edit function:

function edit(e, id) {
    var editdiv = $('#' + id).find('.edit');
    var cntdiv = $('#' + id).find('.content');

    if (editdiv.css('visibility') == 'hidden') {
        editdiv.css('visibility') == 'visible'
        cntdiv.css('visibility') == 'hidden'
        cntdiv.hide();
        editbox.show()
    }
    else {
        editdiv.css('visibility') == 'hidden'
        cntdiv.css('visibility') == 'visible'
        cntbox.show();
        editbox.hide()
    }

    stopEventBubble(e); // Code to cancel event bubbling;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

二智少女 2024-08-25 13:23:18

您是否使用ScriptManager来注册编辑功能?

protected void Page_Load(object sender, EventArgs e)
{
    string jsEdit = @"function edit(event, id) {}";
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "editFunction", jsEdit);
}

如果您的代码位于外部文件中,您可以在页面的 aspx 中使用 ScriptManager 或 ScriptManagerProxy 注册它:

<ScriptManager runat="server" id="ScriptManager1">
    <Scripts>
        <asp:ScriptReference path="~/js/edit.js" />
    </Scripts>
</asp:ScriptManager>

编辑:
好吧,我现在知道问题是什么了。您一开始就没有设置 css 可见性。因此,您需要设置 css 可见性,或者可以修改编辑功能以遵循以下逻辑:

function edit(e, id) {
    var editdiv = $('#' + id).find('.edit');
    var cntdiv = $('#' + id).find('.content');
    //I reversed it to look for visible instead of hidden.  The main problem with this approach and your other approach is that the original value is inherited.
    if (editdiv.css('visibility') == 'visible') {
        editdiv.css('visibility') == 'hidden'
        cntdiv.css('visibility') == 'visible'
        cntbox.show();
        editbox.hide()
    }
    else {
        editdiv.css('visibility') == 'visible'
        cntdiv.css('visibility') == 'hidden'
        cntdiv.hide();
        editbox.show()
    }

    stopEventBubble(e); // Code to cancel event bubbling;
}

另一个选项将要求您在“编辑”和“内容”div 中设置以下内容。

<div id="edit" style="visibility:hidden"> ... </div>
<div id="content" style="visibility:visible"> ... </div>

如果您需要进一步的帮助,我需要查看您有关 UpdatePanel、编辑和内容的 aspx 代码。

Are you using the ScriptManager to register the edit function?

protected void Page_Load(object sender, EventArgs e)
{
    string jsEdit = @"function edit(event, id) {}";
    ScriptManager.RegisterClientScriptBlock(this, GetType(), "editFunction", jsEdit);
}

If your code is in an external file, you can register it with the ScriptManager or the ScriptManagerProxy in the aspx for your page:

<ScriptManager runat="server" id="ScriptManager1">
    <Scripts>
        <asp:ScriptReference path="~/js/edit.js" />
    </Scripts>
</asp:ScriptManager>

EDIT:
alright, I know what the issue is now. You aren't setting the css visibility to begin with. So either you need to set the css visibility or you can modify your edit function to follow the following logic:

function edit(e, id) {
    var editdiv = $('#' + id).find('.edit');
    var cntdiv = $('#' + id).find('.content');
    //I reversed it to look for visible instead of hidden.  The main problem with this approach and your other approach is that the original value is inherited.
    if (editdiv.css('visibility') == 'visible') {
        editdiv.css('visibility') == 'hidden'
        cntdiv.css('visibility') == 'visible'
        cntbox.show();
        editbox.hide()
    }
    else {
        editdiv.css('visibility') == 'visible'
        cntdiv.css('visibility') == 'hidden'
        cntdiv.hide();
        editbox.show()
    }

    stopEventBubble(e); // Code to cancel event bubbling;
}

The other option will require you to set the following in you "edit" and "content" divs.

<div id="edit" style="visibility:hidden"> ... </div>
<div id="content" style="visibility:visible"> ... </div>

If you need further help, I'll need to see your aspx code concerning the UpdatePanel, edit, and content.

迷路的信 2024-08-25 13:23:18

试试这个:

<a href="#" onclick="edit(event,'ctl00_CPH1_ctl00_win1'); return false;">Edit</a>

编辑
作为 克里斯
如果您在单击“编辑”链接时注入该函数,则该函数在您第一次单击“编辑”链接时将不存在。

您可以做的是将函数添加到标记的 部分的

<head>
  <script>
    function edit(event, id) {
      // Your code here
    }
  </script>
</head>

或者添加到单独的 .js 文件中。

Try this:

<a href="#" onclick="edit(event,'ctl00_CPH1_ctl00_win1'); return false;">Edit</a>

Edit
As Chris said:
If you are injecting the function when you click the Edit link the function won't exist the first time you click an Edit link.

What you could do is add the function inside a <script> tag in the <head> section of the markup:

<head>
  <script>
    function edit(event, id) {
      // Your code here
    }
  </script>
</head>

Or in a separate .js file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文