繁体   English   中英

有人可以解释jquery .select()方法吗?

[英]Could someone explain the jquery .select() method?

我看了看文档,发现其他有关此方法的信息时遇到麻烦,我认为它可以处理在文本输入或文本区域中添加或更改文本的事件。 但是,无法使其正常工作。 我希望有人可以进一步向我解释此事件处理程序。

<input id="first" type="text">// if something is typed here an event should be triggered
      //at least if my understanding is correct which it clearly isnt
<input id="second" type="text">//hello should appear here when the event is triggered

<script>
       $("#first").select(function(){$("#second").val("hello")})//this does nothing
</script>

根据official documentation of jquery for .select() ,这里是它的作用定义和示例说明。

官方说明

将事件处理程序绑定到“选择” JavaScript事件,或在元素上触发该事件。

这意味着当您在源元素中select文本时,您正在尝试对目标元素进行某些操作。 当心评论它在做什么。

 //always good to wrap events inside document.ready $(function() { $("#first").select(function() { //once you perform select operation on input#first element either by Ctrl+a //or by Mouse drag event $("#second").val("hello"); //value of input#second element should be updated to hello }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="first" type="text"> <input id="second" type="text"> 

您误解了select方法。 在选择输入元素中的文本时使用。 您的注释表明您希望在键入内容时触发代码。

为此,如果要在元素中收到输入时触发功能,则应使用input事件。

另外,您的函数在second引用的前面没有包含# ,因此您没有正确访问该元素。

 $("#first").on("input", function(){ $("#second").val("hello") }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input id="first" type="text"> <input id="second" type="text"> 

$.select()在文本或textarea元素内选择文本时起作用。 用鼠标“突出显示”文本时的含义。 在下面尝试:

 $("#first").select(function() { $("#second").val("hello"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>First:</div> <input id="first" type="text" value="select this text"> <div>Second:</div> <input id="second" type="text"> 

如果您想添加其他内容,则可以on input使用类似jquery的on input

 $("#first").on('input', function() { $("#second").val("hello"); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div>First:</div> <input id="first" type="text"> <div>Second:</div> <input id="second" type="text"> 

<body>
<input id="first" type="text">
<input id="second" type="text">
<script>
$("#first").change(function() {
    $("#second").val("hello")
})
</script>
</body>

暂无
暂无

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

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