部分回发后更新面板中的 JavaScript 不起作用

发布于 2024-12-10 19:08:50 字数 1662 浏览 0 评论 0原文

 <script type="text/javascript">
        $(function () {
            $('.datePicker').datetimepicker({ dateFormat: 'dd/mm/yy' });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:TextBox ID="TextBox1" class="datePicker" runat="server"></asp:TextBox>
    <asp:UpdatePanel ID="holder" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:DropDownList runat="server" ID="ddl_RespondBy" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
                <asp:ListItem Selected="True">1 Hour</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="txt_RespondBy" class="datePicker" Visible="true" runat="server" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddl_RespondBy" EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>
</asp:Content>

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_RespondBy.SelectedItem.Text == "Other")
        {
            txt_RespondBy.Visible = true;
        }
        else
        {

        }
    }

我使用更新面板进行部分回发,我有两个文本框,一个在更新面板外部,一个在更新面板内部,当我从下拉列表中选择其他文本框并尝试打开 txt_RespondBy 文本框中的日历时,它不会显示,但文本更新面板外部的框显示日历。为什么部分回发后 Javascript 无法在更新面板内工作

 <script type="text/javascript">
        $(function () {
            $('.datePicker').datetimepicker({ dateFormat: 'dd/mm/yy' });
        });
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:TextBox ID="TextBox1" class="datePicker" runat="server"></asp:TextBox>
    <asp:UpdatePanel ID="holder" runat="server" UpdateMode="Always" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:DropDownList runat="server" ID="ddl_RespondBy" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
                <asp:ListItem Selected="True">1 Hour</asp:ListItem>
                <asp:ListItem>Other</asp:ListItem>
            </asp:DropDownList>
            <asp:TextBox ID="txt_RespondBy" class="datePicker" Visible="true" runat="server" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID="ddl_RespondBy" EventName="SelectedIndexChanged" />
        </Triggers>
    </asp:UpdatePanel>
</asp:Content>

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddl_RespondBy.SelectedItem.Text == "Other")
        {
            txt_RespondBy.Visible = true;
        }
        else
        {

        }
    }

I do partial post back with the update panel, I have two text box one outside update panel and one inside, when I select other from the dropdown and try to open the calendar inside the txt_RespondBy text box it doesn't show, but the text box outside update panel shows the calendar. why is Javascript not working inside update panel after partial postback

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

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

发布评论

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

评论(3

牵你手 2024-12-17 19:08:50

将 datetimepicker 初始化代码放入 pageLoad 函数中,每当页面加载(异步或同步)时都会调用该函数。

<script type="text/javascript">
    function pageLoad(sender, args) {
        $('.datePicker').datetimepicker({ dateFormat: 'dd/mm/yy' });
    }      
</script>

Place your datetimepicker initialisation code in the pageLoad function, which is called whenever the page loads (asynchronously or synchronously).

<script type="text/javascript">
    function pageLoad(sender, args) {
        $('.datePicker').datetimepicker({ dateFormat: 'dd/mm/yy' });
    }      
</script>
拥抱我好吗 2024-12-17 19:08:50

您可以使用 pageLoad.live

参考信息:
$(document).ready() 和 pageLoad() 是不一样

.live:

Jquery .live 可以工作,但不能与 .datepicker 一起使用

$(function(){
    $('.datePicker').live('click', function() {
        $(this).datepicker({showOn:'focus'}).focus();
    });
});

pageLoad():

function pageLoad(sender, args) {
    $('.datePicker').datetimepicker({ dateFormat: 'dd/mm/yy' });
}     

You can use pageLoad or .live:

Reference info:
$(document).ready() and pageLoad() are not the same

.live:

Jquery .live works but not with .datepicker

$(function(){
    $('.datePicker').live('click', function() {
        $(this).datepicker({showOn:'focus'}).focus();
    });
});

pageLoad():

function pageLoad(sender, args) {
    $('.datePicker').datetimepicker({ dateFormat: 'dd/mm/yy' });
}     
何必那么矫情 2024-12-17 19:08:50

我知道这是一个老问题,但如果有人现在或以后访问此页面寻求帮助,我也会在这里发布另一个答案。
一个可以使用

$(文档).on()

因为它确保每当页面加载时控件都会绑定到事件。它的工作与页面加载事件类似,但你可以说语法不同。几乎对于任何事情,这都有效。我的面板中有超过 50 个控件,所有控件都执行不同的任务,但我从未遇到过麻烦.. 在我使用此功能时一次也没有遇到过麻烦。

     $(document).on('eventName', 'element' , function(){
     //your code to perform some task; 
});

eventName 可以是任何事件 - 例如单击、更改、选择..

并且

元素 - 可以是名称、类名、id

示例 - 如上面的问题 -

html-

<script src="lib/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<input type="text" class="datePicker" data-provide="datepicker-inline" data-date-format="mm/dd/yyyy">

Javascript-

$(function(){
     $(document).on('click', '.datePicker' , function(){
     $(this).datepicker();
    });
});

I understand this is a old question but then also I am posting one more answer here if someone visit this page now or in later times for help.
one can use

$(document).on()

as it makes sure that controls bind to the events whenever page loads. Its job is Similar to page load event but you can say syntax is different. And almost for anything and everything this works. I have more than 50 controls in a panel and all to perform different task and I never got into trouble.. not even once as i was using this function.

     $(document).on('eventName', 'element' , function(){
     //your code to perform some task; 
});

eventName can be any event - like click, change, select..

And

Element - can be a name, classname, id

example - as above question -

html -

<script src="lib/bootstrap-datepicker/js/bootstrap-datepicker.js"></script>
<input type="text" class="datePicker" data-provide="datepicker-inline" data-date-format="mm/dd/yyyy">

Javascript -

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