简体   繁体   English

我如何 map 特定名称到 JavaScript/jQuery 中的 ID?

[英]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.

相关问题 我如何按 ABC 对 javascript.map 名称数组进行排序 - How can i sort javascript.map array of names by ABC 我有一个包含不同div ID的数组,我如何从aray javascript / jquery中定位特定的ID - I have an array containing different div IDs how do i target specific IDs from within the aray javascript/jquery 如何加载带有ID的javascript模板? - How can i load javascript templates with their ids? 如何使用 JavaScript 创建唯一 ID? - How can I create unique IDs with JavaScript? GTM + JS:如何将ID与类别名称匹配? - GTM + JS: How can I match IDs with Category names? 如何在 jquery/javascript 中剪切包含“map”的字符串 - how can I cut a string that contain “map” in jquery/javascript 如何使用Javascript和Jquery Vector Map设置会话 - How can I set a Session using Javascript with Jquery Vector Map 使用JavaScript和/或JQuery,如何找到“ XPath”或映射到元素? - Using JavaScript and/or JQuery, how can I find the “XPath” or map to an element? 如何映射对象数组的名称以更改字段名称? - How can I map the names of an array of objects to change the field names? 在javascript中使用map替换具有不同名称的ID字符串的最佳方法 - Optimal method to replace string of IDs with different names using map in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM