如何使用jquery通过".val()"获取一个字符串中多个输入的值?

Fir*_*ikh 2 html javascript jquery input

我想.val()在jQuery中使用一个函数一次获取所有输入的值.

$("#txt1").val();
$("#txt2").val();
$("#txt3").val();
Run Code Online (Sandbox Code Playgroud)

而不是这个我想写下面的代码

$("#txt1, #txt2, #txt3").val();
Run Code Online (Sandbox Code Playgroud)

Moh*_*mad 6

使用.map()所选择的输入转换为它们的值,然后使用Array.prototype.join()以阵列结果转换为字符串.

var values = $("#txt1, #txt2, #txt3").map(function(){
  return this.value;
}).get().join(" ");
console.log(values)
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="txt1" value="a" />
<input type="text" id="txt2" value="b" />
<input type="text" id="txt3" value="c" />
Run Code Online (Sandbox Code Playgroud)