[英]How can I map specific names to IDs in JavaScript/jQuery?
Problem: I want to iterate through a specific set of name attributes and append those values to a specific ID.问题:我想遍历一组特定的名称属性和 append 这些值到特定的 ID。
I have a demo that tries to read in a user input and the text to the right will get mirrored, respectively.我有一个尝试读取用户输入的演示,右侧的文本将分别被镜像。
Currently I iterate through my specific name attributes.目前我遍历我的特定名称属性。 However, I now want to append to one respective text box to the right.
但是,我现在想将 append 放到右侧的相应文本框中。 It is appending to all four elements instead.
它改为附加到所有四个元素。
How can I map these elements?我怎样才能 map 这些元素?
$('input[name^="text"]').on("change", function(e){ var user_input = $(this).val(); console.log(user_input); $('input[id^="id_text"').each(function() { let mirrored_value = $(this).val(user_input); }); });
body { padding:10px; } input { margin: 20px 0 0 0; }.mirror{ float: left; margin-left: 20px; }.input{ float: left; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1> The right text box will mirror the left</h1> <div class="input"> <input type="text" name="text1" value=""><br> <input type="text" name="text2" value=""><br> <input type="text" name="text3" value=""><br> <input type="text" name="text4" value=""><br> </div> <div class="mirror"> <input type="text" id="id_text1" value=""><br> <input type="text" id="id_text2" value=""><br> <input type="text" id="id_text3" value=""><br> <input type="text" id="id_text4" value=""> </div>
My preferred method is using a data attribute that refers to the target element and refer to that.我的首选方法是使用引用目标元素并引用它的数据属性。 That way you don't have to worry about changing your javascript if you change your markup.
这样,如果您更改标记,您就不必担心更改 javascript。 Also the
input
event is the better option for text inputs. input
事件也是文本输入的更好选择。
$('input[data-target]').on("input", function(e){ $($(this).data("target")).val($(this).val()); });
body { padding:10px; } input { margin: 20px 0 0 0; }.mirror{ float: left; margin-left: 20px; }.input{ float: left; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1> The right text box will mirror the left</h1> <div class="input"> <input data-target="#id_text1" type="text" name="text1" value=""><br> <input data-target="#id_text2" type="text" name="text2" value=""><br> <input data-target="#id_text3" type="text" name="text3" value=""><br> <input data-target="#id_text4" type="text" name="text4" value=""><br> </div> <div class="mirror"> <input type="text" id="id_text1" value=""><br> <input type="text" id="id_text2" value=""><br> <input type="text" id="id_text3" value=""><br> <input type="text" id="id_text4" value=""> </div>
Using closures and assuming the <input>
element groups are in the same order:使用闭包并假设
<input>
元素组的顺序相同:
const $src = $('.input > input');
const $dst = $('.mirror > input');
$src.each((i,e) => {
const $mirror = $dst.eq(i);
$(e).on('change', e => {
let user_input = $(e.currentTarget).val();
console.log(user_input);
$mirror.val(user_input);
});
});
I would use index if your markup is consistently reliable.如果您的标记始终可靠,我会使用索引。 No IDs or other attributes necessary.
不需要 ID 或其他属性。
$('.input input').on("keyup", function(e){ const idx = $(this).index(); const val = $(this).val(); $('.mirror input').eq(idx).val(val); });
.input, .mirror {display: inline-block; width: 45%;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <h1> The right text box will mirror the left</h1> <div class="input"> <input type="text" name="text1" value=""> <input type="text" name="text2" value=""> <input type="text" name="text3" value=""> <input type="text" name="text4" value=""> </div> <div class="mirror"> <input type="text" value=""> <input type="text" value=""> <input type="text" value=""> <input type="text" value=""> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.