简体   繁体   English

简化Javascript以与多个HTML输入框一起使用

[英]Simplifying Javascript to work with Multiple HTML Input Boxes

I'm trying to make a simple form that will allow a user to type into an HTML text box, which will then send that to another text box that they cannot modify. 我正在尝试制作一个简单的表单,允许用户在HTML文本框中键入内容,然后将其发送到他们无法修改的另一个文本框中。 I have figured out how to do this; 我已经知道如何去做。 however, the code I'm using feels bloated and I'm sure there's a way to streamline it. 但是,我正在使用的代码感到肿,并且我确定有一种方法可以简化它。

Here is my Javascript: 这是我的Javascript:

$(".name_input_1").on('keyup',function(){
    $(".name_print_1").val($(this).val());
});

$(".name_input_2").on('keyup',function(){
    $(".name_print_2").val($(this).val());
});

$(".name_input_3").on('keyup',function(){
    $(".name_print_3").val($(this).val());
});

$(".name_input_4").on('keyup',function(){
    $(".name_print_4").val($(this).val());
});

$(".name_input_5").on('keyup',function(){
    $(".name_print_5").val($(this).val());
});

$(".name_input_6").on('keyup',function(){
    $(".name_print_6").val($(this).val());
});

$(".name_input_7").on('keyup',function(){
    $(".name_print_7").val($(this).val());
});

$(".name_input_8").on('keyup',function(){
    $(".name_print_8").val($(this).val());
});

$(".name_input_9").on('keyup',function(){
    $(".name_print_9").val($(this).val());
});

$(".name_input_10").on('keyup',function(){
    $(".name_print_10").val($(this).val());
});

$(".name_input_11").on('keyup',function(){
    $(".name_print_11").val($(this).val());
});

$(".name_input_12").on('keyup',function(){
    $(".name_print_12").val($(this).val());
});

...and a little more... ...还有更多...

function updateText(type) { 
 var id = type+'_print';
 document.getElementById(id).value = document.getElementById(type).value;
}

Here is some HTML: 这是一些HTML:

    <div style="float:left;">
    <input type="text" class="name_input_1">
    <br>
    <input type="text" class="name_input_2">
    <br>
    <input type="text" class="name_input_3">
    <br>
    <input type="text" class="name_input_4">
    <br>
    <input type="text" class="name_input_5">
    <br>
    <input type="text" class="name_input_6">
    <br>
    <input type="text" class="name_input_7">
    <br>
    <input type="text" class="name_input_8">
    <br>
    <input type="text" class="name_input_9">
    <br>
    <input type="text" class="name_input_10">
    <br>
    <input type="text" class="name_input_11">
    <br>
    <input type="text" class="name_input_12">
    <br>
    </div>
    <div style="float:left;">
    <input type="text" class="name_print_1" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_2" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_3" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_4" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_5" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_6" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_7" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_8" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_9" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_10" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_11" placeholder="Automatically Filled" disabled>
    <br>
    <input type="text" class="name_print_12" placeholder="Automatically Filled" disabled>

I have tried to make a Fiddle for it, but it doesn't work there. 我试图为此做一个小提琴,但是在那儿不起作用。 Not sure why. 不知道为什么。 This does work just fine on a local machine. 这确实可以在本地计算机上正常工作。 I'm just looking for a way to slim-down that code. 我只是在寻找一种简化代码的方法。 Yes, I am a Javascript novice at best. 是的,我充其量只是一名Javascript新手。 Please go easy on me. 请对我好一点。 :) :)

I think this is what you're trying to achieve. 我认为这是您要实现的目标。 The way I wrote it is so that the secoind textbox will update every time a keypress is called on the first textbox, but the code can easily be updated to have the function fire on a different event. 我的编写方式是,每次在第一个文本框上调用按键时,secoind文本框都会更新,但是可以轻松更新代码以使函数在其他事件上触发。

 var nameInputs = document.getElementsByClassName('name_input'); var disabledInputs = document.getElementsByClassName('name_print'); for (let i = 0; i < nameInputs.length; i++) { nameInputs[i].addEventListener('keyup', function() { disabledInputs[i].value = this.value; disabledInputs[i].disabled = true; }); } 
 <div style="float:left;"> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_inpu"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> <input type="text" class="name_input"> <br> </div> <div style="float:left;"> <input type="text" class="name_print" placeholder="Automatically Filled"> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> <br> <input type="text" class="name_print" placeholder="Automatically Filled" disabled> 

Add a single class that corresponds to all inputs, like "name-input", apply a data attribute on inputs pointing towards which will be their "display" and use the function as this: 添加一个与所有输入相对应的单个类,例如“ name-input”,在指向其“显示”的输入上应用data属性,并使用此功能:

$(".name-input").on('keyup',function() {
    var input = $(this);
    var target = input.data('target');
    $(target).val(input.val());
}); 

The data attribute on your HTML will look like this: HTML上的data属性如下所示:

<input type="text" class="name-input name_input_1" data-target=".name_print_1" />

That should help you with getting less javascript code on the expense of adding a few targets on your inputs. 那应该可以帮助您减少JavaScript代码,但要在输入中添加一些目标。

Other than that, you can "hack" your class names and do something like this: 除此之外,您可以“破解”您的类名并执行以下操作:

$('[class^="name_input"]').on('keyup', function () {
    var target = this.className.replace('input', 'print');
    $(target).val($(this).val());
});

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

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