JQuery / JavaScript连接每个键的输入值

时间:2021-02-12 00:19:52

I have some input fields - the count of fields will be dynamic - want to concatenate the values into other field. Concatenate ids from C01 to N and store it in cNum Field. Please help.

我有一些输入字段 - 字段数将是动态的 - 想要将值连接到其他字段。将ID从C01连接到N并将其存储在cNum字段中。请帮忙。

<form class="cardForm" style="padding: 10px;"autocomplete="off" novalidate>
  <div class="cardDiv">
    <input type="hidden" id="C00" class="cardNum" value="" maxlength="1"  />
    <input type="text" id="C01" class="cardNum" value="" maxlength="1" autofocus/>
    <input type="text" id="C02" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C03" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C04" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
    <input type="text" id="C05" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C06" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C07" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C08" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
    <input type="text" id="C09" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C10" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C11" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C12" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
    <input type="text" id="C13" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C14" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C15" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="C16" class="cardNum" value="" maxlength="1"/>
  </div>
  <div>
    <input type="text" id="cNum" class="cardNum" value="" maxlength="1"/>
    <input type="text" id="cStatus" class="cardNum" value="" maxlength="1"/>
  </div>
  <button class="cardReset btn-link" type="reset" value="Reset"><u>Reset</u></button>
</form>

4 个解决方案

#1


0  

In a simple way you can try below code or run the solution on jsfiddle. Explanation: On key up, it will concatenate all value of Text box starting with capital 'C' and result it into 'cNum' text

您可以通过简单的方式尝试下面的代码或在jsfiddle上运行解决方案。说明:在键盘上,它将连接以大写“C”开头的文本框的所有值,并将其导入“cNum”文本

$("input[id^=C]").keyup(function(e) {
  var str = "";
  $("input[id^=C]").each(function(index) {
    str = str + $(this).val()
  });
  
   $("#cNum").val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="cardForm" style="padding: 10px;" autocomplete="off" novalidate>
  <div class="cardDiv">
    <input type="hidden" id="C00" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C01" class="cardNum" value="" maxlength="1" autofocus/>
    <input type="text" id="C02" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C03" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C04" class="cardNum" value="" maxlength="1" />&nbsp;&nbsp;
    <input type="text" id="C05" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C06" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C07" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C08" class="cardNum" value="" maxlength="1" />&nbsp;&nbsp;
    <input type="text" id="C09" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C10" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C11" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C12" class="cardNum" value="" maxlength="1" />&nbsp;&nbsp;
    <input type="text" id="C13" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C14" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C15" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C16" class="cardNum" value="" maxlength="1" />
  </div>
  <div>
    <input type="text" id="cNum" class="cardNum" value="" maxlength="1" />
    <input type="text" id="cStatus" class="cardNum" value="" maxlength="1" />
  </div>
  <button class="cardReset btn-link" type="reset" value="Reset"><u>Reset</u></button>
</form>

#2


0  

If it was me, I would give C01 to C16 an additional class something like

如果是我,我会给C01 C16一个类似的附加课程

<input type="text" id="C01" class="cardNum cardVal" value="" maxlength="1"/>
<input type="text" id="C02" class="cardNum cardVal" value="" maxlength="1"/>
.
.
.
<input type="text" id="C16" class="cardNum cardVal" value="" maxlength="1"/>

and then

var finalString = "";
$(".cardVal").each(function(i, txt){
    finalString += txt.val();
});
$("#cNum").val(finalString);

but if you don't want to do that you can do something like this

但如果你不想这样做,你可以做这样的事情

var finalString = "";
$(".cardNum").each(function(i, txt){

    if(txt.attr("id").indexOf("C") >= 0){
        finalString += txt.val();
    }
});
$("#cNum").val(finalString);

#3


0  

try this

var concatenatedValue = "";

$( ".cardNum" ).each( function(){

  var id  = $( this ).attr( "id" );
  var number = id.substring(1);
  if ( !isNaN( number  ) )
  {
     concatenatedValue += $( this ).val();
  }

} );
alert( concatenatedValue );

#4


0  

You can listen on the keyup event for class "cardNum" and concatenate all inputs under the div "cardDiv" and set to the input box.

您可以侦听类“cardNum”的keyup事件,并连接div“cardDiv”下的所有输入并设置为输入框。

here is the working example.

这是工作的例子。

$(".cardNum").keyup(function() {
  var str = "";
  $(".cardDiv .cardNum").each(function(){
    str += $(this).val();
  });
  $("#cNum").val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="cardForm" style="padding: 10px;"autocomplete="off" novalidate>
<div class="cardDiv">
<input type="hidden" id="C00" class="cardNum" value="" maxlength="1"  />
<input type="text" id="C01" class="cardNum" value="" maxlength="1" autofocus/>
<input type="text" id="C02" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C03" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C04" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
<input type="text" id="C05" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C06" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C07" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C08" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
<input type="text" id="C09" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C10" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C11" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C12" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
<input type="text" id="C13" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C14" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C15" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C16" class="cardNum" value="" maxlength="1"/>
</div>
<div>
<input type="text" id="cNum" class="cardNum" value="" maxlength="1"/>
<input type="text" id="cStatus" class="cardNum" value="" maxlength="1"/>
</div>
<button class="cardReset btn-link" type="reset" value="Reset"><u>Reset</u></button>
</form>

#1


0  

In a simple way you can try below code or run the solution on jsfiddle. Explanation: On key up, it will concatenate all value of Text box starting with capital 'C' and result it into 'cNum' text

您可以通过简单的方式尝试下面的代码或在jsfiddle上运行解决方案。说明:在键盘上,它将连接以大写“C”开头的文本框的所有值,并将其导入“cNum”文本

$("input[id^=C]").keyup(function(e) {
  var str = "";
  $("input[id^=C]").each(function(index) {
    str = str + $(this).val()
  });
  
   $("#cNum").val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="cardForm" style="padding: 10px;" autocomplete="off" novalidate>
  <div class="cardDiv">
    <input type="hidden" id="C00" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C01" class="cardNum" value="" maxlength="1" autofocus/>
    <input type="text" id="C02" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C03" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C04" class="cardNum" value="" maxlength="1" />&nbsp;&nbsp;
    <input type="text" id="C05" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C06" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C07" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C08" class="cardNum" value="" maxlength="1" />&nbsp;&nbsp;
    <input type="text" id="C09" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C10" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C11" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C12" class="cardNum" value="" maxlength="1" />&nbsp;&nbsp;
    <input type="text" id="C13" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C14" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C15" class="cardNum" value="" maxlength="1" />
    <input type="text" id="C16" class="cardNum" value="" maxlength="1" />
  </div>
  <div>
    <input type="text" id="cNum" class="cardNum" value="" maxlength="1" />
    <input type="text" id="cStatus" class="cardNum" value="" maxlength="1" />
  </div>
  <button class="cardReset btn-link" type="reset" value="Reset"><u>Reset</u></button>
</form>

#2


0  

If it was me, I would give C01 to C16 an additional class something like

如果是我,我会给C01 C16一个类似的附加课程

<input type="text" id="C01" class="cardNum cardVal" value="" maxlength="1"/>
<input type="text" id="C02" class="cardNum cardVal" value="" maxlength="1"/>
.
.
.
<input type="text" id="C16" class="cardNum cardVal" value="" maxlength="1"/>

and then

var finalString = "";
$(".cardVal").each(function(i, txt){
    finalString += txt.val();
});
$("#cNum").val(finalString);

but if you don't want to do that you can do something like this

但如果你不想这样做,你可以做这样的事情

var finalString = "";
$(".cardNum").each(function(i, txt){

    if(txt.attr("id").indexOf("C") >= 0){
        finalString += txt.val();
    }
});
$("#cNum").val(finalString);

#3


0  

try this

var concatenatedValue = "";

$( ".cardNum" ).each( function(){

  var id  = $( this ).attr( "id" );
  var number = id.substring(1);
  if ( !isNaN( number  ) )
  {
     concatenatedValue += $( this ).val();
  }

} );
alert( concatenatedValue );

#4


0  

You can listen on the keyup event for class "cardNum" and concatenate all inputs under the div "cardDiv" and set to the input box.

您可以侦听类“cardNum”的keyup事件,并连接div“cardDiv”下的所有输入并设置为输入框。

here is the working example.

这是工作的例子。

$(".cardNum").keyup(function() {
  var str = "";
  $(".cardDiv .cardNum").each(function(){
    str += $(this).val();
  });
  $("#cNum").val(str);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form class="cardForm" style="padding: 10px;"autocomplete="off" novalidate>
<div class="cardDiv">
<input type="hidden" id="C00" class="cardNum" value="" maxlength="1"  />
<input type="text" id="C01" class="cardNum" value="" maxlength="1" autofocus/>
<input type="text" id="C02" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C03" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C04" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
<input type="text" id="C05" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C06" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C07" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C08" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
<input type="text" id="C09" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C10" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C11" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C12" class="cardNum" value="" maxlength="1"/>&nbsp;&nbsp;
<input type="text" id="C13" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C14" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C15" class="cardNum" value="" maxlength="1"/>
<input type="text" id="C16" class="cardNum" value="" maxlength="1"/>
</div>
<div>
<input type="text" id="cNum" class="cardNum" value="" maxlength="1"/>
<input type="text" id="cStatus" class="cardNum" value="" maxlength="1"/>
</div>
<button class="cardReset btn-link" type="reset" value="Reset"><u>Reset</u></button>
</form>