简体   繁体   English

Javascript 多输入值输出

[英]Javascript Multiple Inputs Value Output

Hi I want to create a stamp script and I want the user to enter his name and address in three fields, then he should see the fields later in the stamp edition?嗨,我想创建一个邮票脚本,我希望用户在三个字段中输入他的姓名和地址,然后他应该在邮票版本中看到这些字段? I have 3 input fields where the user can give in his data, now i will give this data in a new class.我有 3 个输入字段,用户可以在其中提供他的数据,现在我将在一个新类中提供这些数据。 This is what i have:这就是我所拥有的:

 window.onload = function() { $( "#Text1" ) .keyup(function() { var value = $( this ).val(); $( ".ausgabe" ).text( value ); }) .keyup(); };
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="Text1"> <input type="text" id="Text2"> <input type="text" id="Text3"> <div class="ausgabe"></div>

As you have not made it very clear what you are trying to accomplish, I am providing a simple example that might send you down the right path.由于您没有明确说明您要完成的工作,我提供了一个简单的示例,可能会让您走上正确的道路。

 $(function() { function updateDiv(source, target) { var newVal = ""; source.each(function() { newVal += "<span class='text " + $(this).attr('id').replace("Text", "item-") + "'>" + $(this).val() + "</span> "; }); target.html(newVal); } $("[id^='Text']").keyup(function(e) { updateDiv($("input[id^='Text']"), $(".ausgabe")); }); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" id="Text1"> <input type="text" id="Text2"> <input type="text" id="Text3"> <div class="ausgabe"></div>

Since you already seem to understand .html() and .由于您似乎已经了解.html()和 . text() , we can look at the Selector. text() ,我们可以看看选择器。 The one used will select all elements with an ID Attribute of Text in the beginning of the string.使用的将选择字符串开头 ID 属性为Text所有元素。 See More: https://api.jquery.com/category/selectors/attribute-selectors/查看更多: https : //api.jquery.com/category/selectors/attribute-selectors/

It looks like you want to mimic what the user is typing in the text inputs and show it in ausgabe .看起来您想模仿用户在文本输入中输入的内容并将其显示在ausgabe If that's what you want, then you can tie the keyUp event to each of the inputs.如果这就是您想要的,那么您可以将keyUp事件绑定到每个输入。

$(input [type='text']).keyUp(function() {
   var value = $(this).val();
   $('.ausgabe').text(value);
}

But this will overwrite .ausgabe every time text is entered into a different input.但是,每次将文本输入到不同的输入中时,这都会覆盖.ausgabe

You could get the value of .ausgabe every time keyUp fires and pre-pend that value:您可以在每次keyUp触发时获取.ausgabe的值并预先添加该值:

So you may want to have a button that renders each input's value into .ausgabe :因此,您可能希望有一个按钮将每个输入的值呈现为.ausgabe

<button>.click(function() {
   $(input[type="text"]).each(function() {
      var boxText = $(this).val(); //text box value
      var aus = $('.ausgabe').text(); //ausgabe value
      $('.ausgabe').text(boxText + ' ' + aus); //combine the current text box value with ausgabe
   })
})

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

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