简体   繁体   English

jQuery获取基于标签的输入字段值

[英]JQuery to get input field value based on label

I need to copy the value of the first input box to the second input box. 我需要将第一个输入框的值复制到第二个输入框。 The id and name of these input boxes are coming from tables. 这些输入框的ID和名称来自表。 What is fix will be the labels like 'Code' and 'Copy Code' 解决的是诸如“代码”和“复制代码”之类的标签

<label>Code</label>
<input type="text" value="1234"  id="custcode_1" />

<label>Copy Code</label>
<input type="text" value=""  id="custcode_2" />

For normal scenario it will be just 对于正常情况,这只是

$('#custcode_2').val($('#custcode_1').val());

But the id or name are generated on the fly so this is not possible. 但是ID或名称是即时生成的,因此这是不可能的。 How to achieve this using labels of the input boxes. 如何使用输入框的标签实现此目的。
Thanks for the help. 谢谢您的帮助。

i think you can store the data value at data- attributes: 我认为您可以将数据值存储在data-属性中:

<label id="code" data-value="1234">
<label id="copy_code" data-value="">

and then copy the value of code to copy_code using 然后使用以下命令将代码的值复制到copy_code

$('#copy_code').data('value', $('#code').data('value'))

You could loop through the inputs and in this loop grab the previous input of the current one. 您可以循环输入,并在此循环中获取当前输入的先前输入。 From there you take it's value. 从那里开始,您就可以拥有价值。

 $('input').each(function(i, e) { var previousSibling = $(this).prevAll('input').first(); if (previousSibling.length) { $(this).val(previousSibling.val()); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label>Code</label> <input type="text" value="1234" id="custcode_1" /> <label>Copy Code</label> <input type="text" value="" id="custcode_2" /> 

You can wrap the elements in a span or div with a class and then use that as your scope. 您可以使用一个类将元素包装在span或div中,然后将其用作范围。

<span class="custWrapper">
<label>Code</label>
<input type="text" value="1234"  id="custcode_1" />

<label>Copy Code</label>
<input type="text" value=""  id="custcode_2" />
</span>

$('.custWrapper').on('load', function(){
    var inpts = $(this).find('input');

    $(inpts[1]).val($(inpts[0]).val());
});

You could try something like this after giving id for both the label's. 给两个标签都提供id后,您可以尝试这样的操作。

HTML: HTML:

<label id='Code'>Code</label>
<input type="text" value="1234"  id="custcode_1" />

<label id='CopyCode'>Copy Code</label>
<input type="text" value=""  id="custcode_2" />

JQuery: jQuery的:

$(function(){

$('label').text('Copy Code').next('input[type=text]').val($('label').text('Code').next('input[type=text]').val());

});

You can use this code, 您可以使用此代码,

//Find the input textbox with label Code
var $copy = $('label').filter(function () {
    return this.firstChild.nodeValue.trim() === 'Code';
}).next('input');

//Find the input textbox with label Copy Code
var $copycode = $('label').filter(function () {
    return this.firstChild.nodeValue.trim() === 'Copy Code';
}).next('input');

//Copy the value
$copycode.val($copy.val());

//Copy the value as character typed into the textbox
$('input').on('keyup',function(){
$copycode.val($copy.val());
});

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

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