Change Text Box Value Based on Select Input with Selected Attribute

后端 未结 7 1142
再見小時候
再見小時候 2020-12-14 23:03

I am attempting to change the value of a text input based on the user selecting a value from a pulldown. I have got it working using the following,



        
7条回答
  •  我在风中等你
    2020-12-15 00:01

    $(document).ready(function() {   
    
      $(document).on("change", "#name", function() {
    
         $("#firstname").val( this.value ); // this.value is enough for you
    
      }).val( $('#firstname').val() ).change(); // for pre-selection trigger
    
    });      
    

    Note

    Instead of .live() use .on() with jQuery 1.7+, because live() is deprecated.

    Syntax of .on() for delegate event handling is:

    $(StaticParent).on( eventName, target, handlerFunction );
    

    Where, StaticParent means the non-dynamic parent container of target element on which you want to bind event.

    So, for above case it would be better to use any static parent of #name instead of document.

提交回复
热议问题