简体   繁体   English

如何让 jquery 脚本“选择”在 asp.net 中工作

[英]How do I get the jquery script "chosen" to work in asp.net

I have this code:我有这个代码:

<script type="text/javascript" src="../Scripts/chosen.min.js" ></script>

[...]

<asp:ListBox class="chosen-select" ID="lbCategory" runat="server" AutoPostBack="true" DataSourceID="SqlDataSourceDropDownListCategory" 
SelectionMode="Multiple" Width="200px" DataTextField="Name" DataValueField="IdCategory"></asp:ListBox>

<script>
    $(".chosen-select").chosen();
</script>

I have the js in the path that I wrote and the script dosen't work, I tried to search, but everthing I found doesn't fix it, could someone help please.我在我写的路径中有js,脚本不起作用,我试图搜索,但我发现的一切都没有解决它,请有人帮忙。

What I see as issues.我认为的问题。

  1. There is no chosen.min.js in the files.文件中没有chosen.min.js There is the chosen.jquery.min.js and some others.chosen.jquery.min.js和其他一些。
  2. The AutoPostBack="true" will not work if you use the chosen plugin because the plugin redesign the original control witch now is hidden.如果您使用所选插件,则AutoPostBack="true"将不起作用,因为该插件重新设计了原来的控制女巫现在是隐藏的。 Also you use SelectionMode="Multiple" so if the auto post back work in every click you going to have a post back, this is not the best expirience for web.此外,您使用SelectionMode="Multiple" ,因此如果自动回发在每次点击时都有效,那么您将获得回发,这不是 web 的最佳体验。
  3. You do not include the jQuery library that chosen needs to work.您不包括选择需要工作的jQuery library
  4. Prefer CssClass and not class .更喜欢CssClass而不是class Both works but for asp.net controls the CssClass is the correct one to avoid bugs.两者都有效,但对于 asp.net 控件, CssClass是避免错误的正确方法。

How to find the errors.如何找到错误。 Open the browser debug tools by right click on page, on the open menu select Inspect and then open Console and see what errors do you have get and take it from there.通过右键单击页面打开浏览器调试工具,在打开菜单 select Inspect ,然后打开Console ,看看你有什么错误并从那里获取。

Reference参考

Chosen documentation选择的文件
Chosen downloads选择下载

Working Example工作示例

I create a minimum example and test it and works.我创建了一个最小的示例并对其进行测试并可以正常工作。

<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script type="text/javascript" src="/js/jQuery/Chosen/chosen.jquery.js " ></script>
<link rel="stylesheet" href="/js/jQuery/Chosen/chosen.css" />               

<asp:ListBox ID="lstCategoryType" runat="server" CssClass="chosen-select"  SelectionMode="Multiple" Width="200px">
    <asp:ListItem Text="Option 1" Value="1"></asp:ListItem>
    <asp:ListItem Text="Option 2" Value="2"></asp:ListItem>
    <asp:ListItem Text="Option 3" Value="3"></asp:ListItem>
    <asp:ListItem Text="Option 4" Value="4"></asp:ListItem>
</asp:ListBox>

<script>
    jQuery(document).ready(function(){
        jQuery(".chosen-select").chosen();    
    });
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM