简体   繁体   English

jQuery on change选择字段填充输入文本字段

[英]jQuery on change select field populate input text field

I want to take the value from each select field either by name selector or id and input that value into input text field again either by name selector or id. 我想通过名称选择器或id从每个选择字段中获取值,然后通过名称选择器或id将值再次输入到输入文本字段中。

Note the 0 at the end of id that can go from 0 to ++ number count up. 请注意,id末尾的0可以从0到++数字递增。 I want the value of this select field to be input in the input text field on value change. 我希望此选择字段的值在值更改时输入到输入文本字段中。

<select name="option_tree[orn_category_background][0][orn_cat_bg_select]" id="orn_category_background_orn_cat_bg_select_0" class="option-tree-ui-select ">

<input type="text" name="option_tree[orn_category_background][0][title]" id="orn_category_background_title_0" value="VALUE FROM SELECT HERE" class="widefat option-tree-ui-input option-tree-setting-title">

you could use jQuery selector, like, do: 您可以使用jQuery选择器,例如:

$("select[id^='orn_category_background_orn_cat_bg_select_']").change(function() {
   var $this = $(this), 
    idNum = $.attr("id").split("_").pop(); //get the last num 0, 1, etc
   $("input[id='orn_category_background_title_"+idNum+"']").val($this.val());
});

Demo:: jsFiddle 演示:: jsFiddle

Here is a simple method that you can tailor to your needs. 这是一个简单的方法,您可以根据需要进行调整。

HTML Part HTML部分

<select name="option_tree[orn_category_background][0][orn_cat_bg_select]" id="orn_category_background_orn_cat_bg_select_0" class="option-tree-ui-select getOption">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<input type="text" name="option_tree[orn_category_background][0][title]" id="orn_category_background_title_0" class="widefat option-tree-ui-input option-tree-setting-title returnValue">

jQuery Part jQuery部分

$('.getOption').change(function () {
   $( "select option:selected" ).each(function() {
         $('.returnValue').val($( this ).text());
    });

});

Remember to add the new classes to your html elements. 请记住将新类添加到html元素中。

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

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