我有一个输入类型文本作为以下代码
<input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" > <input type="text" minlength="1" maxlength="1" class="myinputs" name="myinputs[]" >
我使用这些输入值如下
//submitotp is my submit button
$("#submitotp").click(function(){
var otp = $(".myinputs").
//HERE i WANTS TO COMBINE ALL 4 Input
}
例如如果
Input-1 = 5 Input-2 = 4 Input-3 = 9 Input-4 = 2 var otp = 5492 //THIS IS O/p
现在我想要的是将所有输入值组合成一个.为此,我引用这个link.但没有得到任何确切的想法.还有关于jQuery.merge()的hearse,但没有帮助或不理解.那我该怎么做呢?
解决方法
您可以使用.each()循环,获取值并将其存储/推送到数组变量中.你可以使用join()加入数组
您还必须将您的类重命名为myinputs而不使用[]
$(document).ready(function() {
$("#submitotp").click(function() {
var otp = [];
$(".myinputs").each(function() {
otp.push($(this).val());
});
console.log(otp.join(""));
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="text" minlength="1" maxlength="1" class="myinputs"> <input type="text" minlength="1" maxlength="1" class="myinputs"> <input type="text" minlength="1" maxlength="1" class="myinputs"> <input type="text" minlength="1" maxlength="1" class="myinputs"> <button id="submitotp" type="button">Click Me!</button>
http://api.jquery.com/jquery.each/