猿问

从另一个输入字段的下拉列表中自动获取值

我有一个选择下拉菜单,可以从我公司不同部门的用户那里获取邮件。我想,当用户从下拉列表中选择部门名称时,部门的邮件地址将自动设置在下一个输入字段中。


<select class="form-control" id="mail_type" name="email_to" onChange="getMailtype(this.value)" required>

  <option value="support@mail.com" selected>Support</option>

  <option value="bill@mail.com">Billing</option>

  <option value="sales@mail.com">Sales</option>

 </select>


 <span id="mail_info">

 <input type="text" name="email" class="form-control" value="support@mail.com" readonly>

  </span>

           

所以,在这里我选择一个选项,电子邮件输入字段将使用 js 自动获取值


Cats萌萌
浏览 96回答 2
2回答

UYOU

获取input并设置的值selectfunction getMailtype(value) {&nbsp; document.querySelector("#mail_info input").value = value;}<select class="form-control" id="mail_type" name="email_to" onChange="getMailtype(this.value)" required>&nbsp; <option value="support@mail.com" selected>Support</option>&nbsp; <option value="bill@mail.com">Billing</option>&nbsp; <option value="sales@mail.com">Sales</option></select><span id="mail_info">&nbsp;<input type="text" name="email" class="form-control" value="support@mail.com" readonly>&nbsp; </span>

森林海

const getMailtype = (target) => {&nbsp; $("#mail_info > input[name='email'")[0].value = target.value;}<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><select class="form-control" id="mail_type" name="email_to" onChange="getMailtype(this)" required>&nbsp; <option value="support@mail.com" selected>Support</option>&nbsp; <option value="bill@mail.com">Billing</option>&nbsp; <option value="sales@mail.com">Sales</option></select><span id="mail_info">&nbsp;<input type="text" name="email" class="form-control" value="support@mail.com" readonly>&nbsp; </span>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答