使用Javascript将多个输入字段合并为一个输入字段可以通过以下步骤实现:
<form>
<input type="text" id="firstName" placeholder="First Name">
<input type="text" id="lastName" placeholder="Last Name">
<input type="email" id="email" placeholder="Email">
<button type="button" onclick="mergeFields()">Merge Fields</button>
</form>
function mergeFields() {
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var email = document.getElementById("email").value;
var mergedField = firstName + " " + lastName + " (" + email + ")";
// 将合并后的值设置到一个新的输入字段中
document.getElementById("mergedField").value = mergedField;
}
<input type="text" id="mergedField" readonly>
完整的代码如下:
<form>
<input type="text" id="firstName" placeholder="First Name">
<input type="text" id="lastName" placeholder="Last Name">
<input type="email" id="email" placeholder="Email">
<button type="button" onclick="mergeFields()">Merge Fields</button>
</form>
<input type="text" id="mergedField" readonly>
<script>
function mergeFields() {
var firstName = document.getElementById("firstName").value;
var lastName = document.getElementById("lastName").value;
var email = document.getElementById("email").value;
var mergedField = firstName + " " + lastName + " (" + email + ")";
document.getElementById("mergedField").value = mergedField;
}
</script>
这样,当用户在输入字段中输入值后,点击"Merge Fields"按钮,就会将多个输入字段的值合并为一个,并显示在新的输入字段中。
注意:以上代码只是一个简单的示例,实际应用中可能需要根据具体需求进行适当的修改和验证。
领取专属 10元无门槛券
手把手带您无忧上云