简体   繁体   中英

Jquery - value from dropdown select to previous input field element

I am trying to assign a value from a dropdown input ( select ) to a simple text input .

I have a simple markup like so :

<p>
    <input class="large-text o99-input-from-dropdown" id="o99_coks_settings[coks_banner_link_href]"  name="o99_coks_settings[coks_banner_link_href]" type="text" value="some-php-generated-value"/>
    <label class="description" for="o99_coks_settings[coks_banner_link_href]">
    </label>
</p>
<p>
    <label for="o99_coks_settings[coks_page_href]"></label>
    <select name="o99_coks_settings[coks_page_href]" id="o99_coks_settings[coks_page_href]" class="o99-dropdown-2-input">
    <option value='false'>None</option>
    <option value='some-php-generated-values'>Some other PHP generated values </option>
    ....  
    ....  n
    </select>
</p>

Yes, I know the id's of type id[array] are not 100% valid, but unfortunately this is 3rd party requirement.

The following is my jQuery that is (should be) in fact super-simple one-liner - but not functioning ( with some trials that all come up with " undefined " )

 jQuery('.o99-dropdown-2-input').change(function() {
  var val = jQuery(this).val(); 
 // OR
 //  var val = this.value;

  var val2 =  jQuery(this).prevAll('input[type=text]:first').val();
  var val3 =  jQuery(this).prevAll('input').val();
  var v = jQuery(this).prev(".o99-input-from-dropdown").find("input").val()

  console.log('Previous input id ' + val3.attr("id")); // all undefined

  jQuery(this).prevAll('input[type=text]:first').val = "testVal1";// all undefined
  jQuery(this).prevAll('.o99-input-from-dropdown').val = "testVal2";// all undefined
  jQuery(this).prevAll('.o99-input-from-dropdown').find('input[type=text]:first').val = "testVal3";// all undefined
  jQuery(this).parent().prev().find('input[type=text]:first').val = "testVal4";// all undefined
  jQuery(this).prev('.o99-input-from-dropdown').find('input').val = "testVal5";// all undefined


  console.log(v);

})

All of the above trials ( and some other combinations ) are returning undefined -What am I missing here ?

Try this:

  console.log('Previous input id ' + $(val3).attr("id")); 

  jQuery(this).prevAll('input[type=text]:first').val("testVal1");
  jQuery(this).prevAll('.o99-input-from-dropdown').val("testVal2");
  jQuery(this).prevAll('.o99-input-from-dropdown').find('input[type=text]:first').val("testVal3");
  jQuery(this).parent().prev().find('input[type=text]:first').val("testVal4") ;jQuery(this).prev('.o99-input-from-dropdown').find('input').val("testVal5");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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