代码>标签{ 显示:块; } .隐藏{ 显示:无; } 基本信息 姓名: 电邮: 工作角色 全栈JavaScript开发人员 前端显影剂 后端开发人员 设计师 学生 其他 其他

代码>标签{ 显示:块; } .隐藏{ 显示:无; } 基本信息 姓名: 电邮: 工作角色 全栈JavaScript开发人员 前端显影剂 后端开发人员 设计师 学生 其他 其他,javascript,html,forms,dom,drop-down-menu,Javascript,Html,Forms,Dom,Drop Down Menu,您的代码中有太多错误。但我已经找到了答案,并想出了解决办法。用这个 <fieldset class="basic"> <legend>Basic Info</legend> <label for="name">Name:</label> <input type="text" id="name" name="user_name"> <l

您的代码中有太多错误。但我已经找到了答案,并想出了解决办法。用这个

<fieldset class="basic">         
        <legend>Basic Info</legend>

        <label for="name">Name:</label>
        <input type="text" id="name" name="user_name">

        <label for="mail">Email:</label>
        <input type="email" id="mail" name="user_email">

        <label>Job Role</label>
        <select id="title" name="user_title">
          <option value="full-stack js developer">Full Stack JavaScript Developer</option>
          <option value="front-end developer">Front End Developer</option>
          <option value="back-end developer">Back End Developer</option>
          <option value="designer">Designer</option>          
          <option value="student">Student</option>
          <option value="other">Other</option>  
        </select>           
      </fieldset>
      <script>
'use strict';

// Hold DOM elements for easy access
var pageBody = document.querySelector('body');
var jobRoleSelect = document.getElementById('title');
//console.log(jobSelected);
var basicSelection = document.querySelector('.basic');
//console.log(basicSection);

jobRoleSelect.onchange = function(){
    var jobSelected = jobRoleSelect.options[jobRoleSelect.selectedIndex].value;
// Job Role section of the form. Reveal a text field when the "Other" option is selected from the "Job Role" drop down menu
if(jobSelected == 'other') {
    console.log('nice');
    var otherText = document.createElement('input');
    // Add an text input field. Use the id of "other-title" 
    otherText.setAttribute('id', 'other-title');
    otherText.setAttribute('type', 'text');
    otherText.setAttribute('name', 'other_field');
    otherText.setAttribute('placeholder', 'Your Title');

    var otherLabel = document.createElement('label');
    otherLabel.setAttribute('for', 'other_field');
    otherLabel.innerHTML = 'other';

    basicSelection.appendChild(otherLabel);
    basicSelection.appendChild(otherText);
}
}
</script>

基本信息
姓名:
电邮:
工作角色
全栈JavaScript开发人员
前端显影剂
后端开发人员
设计师
学生
其他
"严格使用",;
//保存DOM元素以便于访问
var pageBody=document.querySelector('body');
var jobRoleSelect=document.getElementById('title');
//console.log(已选择作业);
var basicSelection=document.querySelector('.basic');
//console.log(基本部分);
jobRoleSelect.onchange=函数(){
var jobSelected=jobRoleSelect.options[jobRoleSelect.selectedIndex]。值;
//表单的“职务角色”部分。从“职务角色”下拉菜单中选择“其他”选项时,显示文本字段
如果(所选作业==“其他”){
console.log('nice');
var otherText=document.createElement('input');
//添加文本输入字段。使用“其他标题”的id
setAttribute('id','other title');
setAttribute('type','text');
setAttribute('name','other_字段');
setAttribute(“占位符”、“您的标题”);
var otherLabel=document.createElement('label');
setAttribute('for','other_字段');
otherLabel.innerHTML='other';
basicSelection.appendChild(其他标签);
基本选择。附加子项(其他文本);
}
}

这里有一个工作

您的代码中有很多错误。但我已经找到了答案,并想出了解决办法。用这个

<fieldset class="basic">         
        <legend>Basic Info</legend>

        <label for="name">Name:</label>
        <input type="text" id="name" name="user_name">

        <label for="mail">Email:</label>
        <input type="email" id="mail" name="user_email">

        <label>Job Role</label>
        <select id="title" name="user_title">
          <option value="full-stack js developer">Full Stack JavaScript Developer</option>
          <option value="front-end developer">Front End Developer</option>
          <option value="back-end developer">Back End Developer</option>
          <option value="designer">Designer</option>          
          <option value="student">Student</option>
          <option value="other">Other</option>  
        </select>           
      </fieldset>
      <script>
'use strict';

// Hold DOM elements for easy access
var pageBody = document.querySelector('body');
var jobRoleSelect = document.getElementById('title');
//console.log(jobSelected);
var basicSelection = document.querySelector('.basic');
//console.log(basicSection);

jobRoleSelect.onchange = function(){
    var jobSelected = jobRoleSelect.options[jobRoleSelect.selectedIndex].value;
// Job Role section of the form. Reveal a text field when the "Other" option is selected from the "Job Role" drop down menu
if(jobSelected == 'other') {
    console.log('nice');
    var otherText = document.createElement('input');
    // Add an text input field. Use the id of "other-title" 
    otherText.setAttribute('id', 'other-title');
    otherText.setAttribute('type', 'text');
    otherText.setAttribute('name', 'other_field');
    otherText.setAttribute('placeholder', 'Your Title');

    var otherLabel = document.createElement('label');
    otherLabel.setAttribute('for', 'other_field');
    otherLabel.innerHTML = 'other';

    basicSelection.appendChild(otherLabel);
    basicSelection.appendChild(otherText);
}
}
</script>

基本信息
姓名:
电邮:
工作角色
全栈JavaScript开发人员
前端显影剂
后端开发人员
设计师
学生
其他
"严格使用",;
//保存DOM元素以便于访问
var pageBody=document.querySelector('body');
var jobRoleSelect=document.getElementById('title');
//console.log(已选择作业);
var basicSelection=document.querySelector('.basic');
//console.log(基本部分);
jobRoleSelect.onchange=函数(){
var jobSelected=jobRoleSelect.options[jobRoleSelect.selectedIndex]。值;
//表单的“职务角色”部分。从“职务角色”下拉菜单中选择“其他”选项时,显示文本字段
如果(所选作业==“其他”){
console.log('nice');
var otherText=document.createElement('input');
//添加文本输入字段。使用“其他标题”的id
setAttribute('id','other title');
setAttribute('type','text');
setAttribute('name','other_字段');
setAttribute(“占位符”、“您的标题”);
var otherLabel=document.createElement('label');
setAttribute('for','other_字段');
otherLabel.innerHTML='other';
basicSelection.appendChild(其他标签);
基本选择。附加子项(其他文本);
}
}

这里有一个工作

你的JS代码从哪里调用?出于您的目的,它应该位于从select元素上的更改事件处理程序调用的函数中。要使用
.querySelector()
选择类为
'basic'
的元素,需要在类名前放置
,如
文档.querySelector('.basic')
。此外,您还可以更简单地获取select元素的选定值:只需使用
jobRoleSelect.value
,无需通过
.options
集合。(另外,与您遇到的问题无关,标签中的
for
属性应设置为关联元素的
id
,而不是其
名称
)感谢您为querySelector找到缺少的
,我忘了这一点。我已经在HTML的正文底部附加了JS。我知道它已经连接好了,因为另一个功能工作正常。所以我可以只使用:
var jobSelected=jobRoleSelect.value',然后在if条件下,使用
if(jobSelected=='other')`?从哪里调用JS代码?出于您的目的,它应该位于从select元素上的更改事件处理程序调用的函数中。要使用
.querySelector()
选择类为
'basic'
的元素,需要在类名前放置
,如
文档.querySelector('.basic')
。此外,您还可以更简单地获取select元素的选定值:只需使用
jobRoleSelect.value
,无需通过
.options
集合。(另外,与您遇到的问题无关,标签中的
for
属性应设置为关联元素的
id
,而不是其
名称
)感谢您为querySelector找到缺少的
,我忘了这一点。我已经在HTML的正文底部附加了JS。我知道它已经连接好了,因为另一个功能工作正常。所以我可以只使用:
var jobSelected=jobRoleSelect.value',然后在if条件下,使用
if(jobSelected=='other')`?
var jobRoleSelect = document.getElementById('title');
jobRoleSelect.addEventListener('change', function() {
  // your other code here
});
<fieldset class="basic">         
        <legend>Basic Info</legend>

        <label for="name">Name:</label>
        <input type="text" id="name" name="user_name">

        <label for="mail">Email:</label>
        <input type="email" id="mail" name="user_email">

        <label>Job Role</label>
        <select id="title" name="user_title">
          <option value="full-stack js developer">Full Stack JavaScript Developer</option>
          <option value="front-end developer">Front End Developer</option>
          <option value="back-end developer">Back End Developer</option>
          <option value="designer">Designer</option>          
          <option value="student">Student</option>
          <option value="other">Other</option>  
        </select>           
      </fieldset>
      <script>
'use strict';

// Hold DOM elements for easy access
var pageBody = document.querySelector('body');
var jobRoleSelect = document.getElementById('title');
//console.log(jobSelected);
var basicSelection = document.querySelector('.basic');
//console.log(basicSection);

jobRoleSelect.onchange = function(){
    var jobSelected = jobRoleSelect.options[jobRoleSelect.selectedIndex].value;
// Job Role section of the form. Reveal a text field when the "Other" option is selected from the "Job Role" drop down menu
if(jobSelected == 'other') {
    console.log('nice');
    var otherText = document.createElement('input');
    // Add an text input field. Use the id of "other-title" 
    otherText.setAttribute('id', 'other-title');
    otherText.setAttribute('type', 'text');
    otherText.setAttribute('name', 'other_field');
    otherText.setAttribute('placeholder', 'Your Title');

    var otherLabel = document.createElement('label');
    otherLabel.setAttribute('for', 'other_field');
    otherLabel.innerHTML = 'other';

    basicSelection.appendChild(otherLabel);
    basicSelection.appendChild(otherText);
}
}
</script>