我有一个jQuery
函数,它的意思是在asp.net文本框中
自动选择文本。但是,文本框中的文本已被选中,但立即取消选择。
如果我使用.focus(function())绑定到焦点事件,该代码是有效的,但是我将文本框动态添加到页面,这就是为什么我认为我需要使用live事件。
任何人都可以看到问题吗?在多视图中的两个网格视图的项目模板中,有问题的文本框是否有所区别?
码:
- <script type="text/javascript">
-
- //Select all text in Cost Rate Text Boxes when they have focus
- $(document).ready(function () {
- $(".CostRateTextBox").live('focus',function () {
- $(this).select();
- });
-
- });
-
- </script>
编辑:
- <script type="text/javascript">
-
- //Select all text in Cost Rate Text Boxes when they have focus
- $(document).ready(function () {
- $(".CostRateTextBox").live('focus',function () {
- $(this).select();
- preventDefault();
- });
-
- });
-
- </script>
这似乎是mouseup事件的干扰。您会注意到,如果您单击并按住表单字段,然后将其移动到“mouseup”选择棒。使用mouseup而不是焦点来触发select()
方法似乎很好:
- <script type="text/javascript">
-
- //Select all text in Cost Rate Text Boxes when they have focus
- jQuery(function($){
- $("table.demo").on("mouseup",".CostRateTextBox",function () {
- $(this).select();
- });
- });
-
- </script>
演示:jsfiddle.net/gableroux/jvJzX/12
请参阅original demo的jQuery 1.3 – 1.8兼容代码。