【问题标题】:retrieve id of textbox from content place holder in jquery function从 jquery 函数中的内容占位符中检索文本框的 id
【发布时间】:2016-06-06 01:59:15
【问题描述】:

我正在为我的网页使用 JQUERY UI。我正在尝试使用 datepicker 控件。问题是 jquery 函数没有使用文本框 ID。当我将内容占位符 ID 与文本框 ID 连接时,它正在工作。

<asp:Content ID="PassportContent" ContentPlaceHolderID="CPH001" runat="server">
<div class="form-group">
    <label>Date Of Birth</label>
    <asp:TextBox ID="datepicker" runat="server" CssClass="txt-control"></asp:TextBox>
</div>
</asp:Content>

实际jquery代码

<script src="../jquery/external/jquery/jquery.js"></script>
<script src="../jquery/jquery-ui.min.js"></script> 
<script>
    $(function () {
        $("#datepicker").datepicker();
    });
</script>

工作 jquery 代码

<script>
    $(function () {
        var temp = document.getElementById('<%= datepicker.ClientID %>').value;
        alert(temp);
        $("#CPH001_datepicker").datepicker();
    });
</script>

当我在论坛中搜索大多数建议使用 document.getElementById 的解决方案时,我尝试使用 >>

获取 ID

document.getElementById('&lt;%= datepicker.ClientID %&gt;').value;但警告框显示为 NULL。

  1. 如何解决这个问题?
  2. 当我将我的 js 代码保存在单独的文件中时,如何解决这个问题?

按照建议,我使用了$("[id$=datepicker]").datepicker();。它有效,我得到了日期选择器。但我无法更改日期格式并限制日期范围。请建议。

<script>
    $(function () {
        $("[id$=datepicker]").datepicker();        

        $("[id$=datepicker]").datepicker({ dateFormat: 'ISO 8601 - yy-mm-dd' }); // not working
    });
    // not working
    $("#format").change(function () { $("[id$=datepicker]").datepicker('ISO 8601 - yy-mm-dd', "dateFormat", $(this).val()); });
</script>

【问题讨论】:

  • 您可以在 asp:TextBox 中设置 ClientIDMode="Static"。这将创建具有定义的实际 id 的渲染元素,然后您可以使用 $("#datepicker").datepicker();

标签: javascript asp.net jquery-ui contentplaceholder


【解决方案1】:

当内容页面呈现Textbox id 时发生变化。像这样使用Textbox id

<script>
$(function () {
    $("[id$=datepicker]").datepicker();
});
</script>

How to use JQuery with Master Pages?

【讨论】:

  • 感谢您的回复。有用。一个问题>>当我将 js 代码保存在单独的文件中时它是否也可以工作?另外,我尝试限制日期范围,但代码不起作用。 $("[id$=datepicker]").datepicker(); $("[id$=datepicker]").datepicker({ minDate: -20, maxDate: "+1M +10D" }); $("#format").change(function () { $("[id$=datepicker]").datepicker('ISO 8601 - yy-mm-dd', "dateFormat", $(this).val()); });
  • 是的,如果您将 js 保存在单独的文件中,它将起作用。但我对日期范围感到困惑。您要显示哪个年份/日期范围?例如,如果您想使用 1990 年来呈现,请使用类似 ` $(function () { $("[id$=datepicker]").datepicker({ changeMonth: true, changeYear: true, dateFormat: 'dd/mm /yy', yearRange: "1990:+0" }); });`
【解决方案2】:

用输入框试试,你的代码是正确的:

<input type="text" id="datepicker" runat="server" CssClass="txt-control" />

我不确定 jquery 是否可以处理“asp:TextBox”标签还是会被替换?

【讨论】:

  • b/n asp:TextBoxinput 有什么区别。 input 如何影响我的代码。
  • 据我了解,这段代码不起作用$("#datepicker").datepicker();...也许它需要一个输入标签,但我不确定,就像我说的那样,你可以试试。
猜你喜欢
  • 2011-07-29
  • 1970-01-01
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 2017-01-14
  • 2012-12-09
  • 2023-04-08
  • 2015-12-15
相关资源
最近更新 更多