我有一个小jQuery函数,用于在获得焦点时自动选择asp.net文本框中的文本.但是,文本框中的文本会被选中,但会立即取消选择.
如果我使用.focus(function())绑定到焦点事件,代码可以工作,但我正在动态地将文本框添加到页面,这就是为什么我认为我需要使用直播事件.
任何人都可以看到问题吗?有问题的文本框是多视图内两个网格视图的项目模板,如果这有所不同?
码:
<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>
Run Code Online (Sandbox Code Playgroud)

编辑:
<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>
Run Code Online (Sandbox Code Playgroud)
Mar*_*cel 25
这似乎是mouseup事件的干扰.您会注意到,如果您在表单字段中单击并按住,然后移出它以外的mouseup"选择棒".使用mouseup而不是focus触发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>
Run Code Online (Sandbox Code Playgroud)
请参阅jQuery 1.3 - 1.8兼容代码的原始演示.
| 归档时间: |
|
| 查看次数: |
14989 次 |
| 最近记录: |