JQuery Onselect?

别说谁变了你拦得住时间么 提交于 2019-12-12 03:53:20

问题


When I type in DPRtelephonenumber I want to repeat it in DPRcallerhometelephonenumber.

This script works as long as I am typing. But if <INPUT id="DPRtelephonenumber"> offers a drop-down from a previously used value, AND I select it, <INPUT id="DPRcallerhometelephonenumber"> does not get set.

So, what do I need more than a keyup? How do I write in an onselect??

<input name="DPRtelephonenumber" id="DPRtelephonenumber" type="text">
<input name="DPRcallerhometelephonenumber" id="DPRcallerhometelephonenumber" type="text">
<script type="text/javascript">
  $(document).ready(function() {
    $('#DPRtelephonenumber').keyup(function() {
      $('#DPRcallerhometelephonenumber').val($(this).val());
    });
  });    
</script>

回答1:


Since you are dealing with a text input element, I assume the drop-down is just the auto-completer supplied by the browser. There is no way to attach an "onselect" handler to that. You could at least attach an on-change handler to the input element.

Try changing:

$('#DPRtelephonenumber').keyup(function() {

To:

$('#DPRtelephonenumber').on('change keyup', function() {

Of course, the change event doesn't fire until the text field loses focus. If you want your event handler to execute as soon as the value of the text field changes, you could check out this question.




回答2:


You can use the onchange event for drop down menu's.

$('#mySelect').change(function() {
  // do stuff here;
});



回答3:


Something like this should do what you want:

<script type="text/javascript">
    $(function(){
        var updateHomeNum = function(obj){
            $("#DPRcallerhometelephonenumber").val(obj.val());
        };
        $("#DPRtelephonenumber").on({
            keyup: function(){
                updateHomeNum($(this));
            }, change: function(){
                updateHomeNum($(this));
            }
        });
    });
</script>


来源:https://stackoverflow.com/questions/15869640/jquery-onselect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!