3 回答

TA貢獻1812條經驗 獲得超5個贊
無需傳入thiseventHandler。一個event對象會自動傳遞給您的處理程序。您需要做的是捕獲觸發事件的元素的值。你的功能應該是這樣的
function showOtherJobRole(event) {
if (event.target.value == "other-title") {
document.getElementById("showOtherJobRole").style.display = "block";
}
else {
document.getElementById("showOtherJobRole").style.display = "none";
}
}
和你的 html 這樣
<fieldset>
<label for="title">Job Role</label>
<select id="title" name="user-title" onchange="showOtherJobRole">
<option value="" disabled="disabled" selected>--</option>
<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-title" id="other-title">Other</option>
</select>
</fieldset>
<div id="showOtherJobRole" style="display: none">
<textarea name="otherJobText" id="otherJobText" cols="40" placeholder="Your Job Role" rows="4"></textarea>
</div>
試試這個讓我們看看它是否有效

TA貢獻1810條經驗 獲得超4個贊
我認為這就是您要找的東西。我做到了,當你選擇它時,Designer它會顯示帶有 ID 的 div showDiv。
HTML
<div id="showDiv">
<p>some content</p>
</div>
<select id="title" name="user-title">
<option value="" disabled="disabled" selected>--</option>
<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 id="other-title">Other</option>
</select>
我刪除了您使用的 JS 代碼,因為我認為這更適合您的需求。你必須自定義它。
JavaScript
const source = document.querySelector("#title");
const target = document.querySelector("#showDiv");
const displayWhenSelected = (source, value, target) => {
const selectedIndex = source.selectedIndex;
const isSelected = source[selectedIndex].value === value;
target.classList[isSelected
? "add"
: "remove"
]("show");
};
source.addEventListener("change", (evt) =>
displayWhenSelected(source, "designer", target)
);
CSS
#showDiv {
display: none;
}
#showDiv.show {
display: block;
}

TA貢獻1799條經驗 獲得超6個贊
首先,檢查您的 HTML 部分。您沒有“其他”選項的值屬性。所以,添加一個值屬性。其次,你不需要傳遞“this”。第三,您不需要為其他選項添加 id,因為您可以通過名為“title”的選擇 id 來完成您的工作。所以,長話短說調用函數 onChange select。通過getElementById函數獲取選中選項的值,比較其值是否等于“other-title”。如果是“other-title”,則顯示 div 文本區域,否則不顯示。
function showOtherJobRole() {
let nameOfJob = document.getElementById("title").value;
if (nameOfJob === "other-title") {
document.getElementById("showOtherJobRole").style.display = "block";
} else {
document.getElementById("showOtherJobRole").style.display = "none";
}
}
<fieldset>
<label for="title">Job Role</label>
<select id="title" name="user-title" onchange="showOtherJobRole();">
<option value="" disabled="disabled" selected>--</option>
<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-title">Other</option>
</select>
</fieldset>
<div id="showOtherJobRole" style="display: none">
<textarea name="otherJobText" id="otherJobText" cols="40" placeholder="Your Job Role" rows="4"></textarea>
</div>
添加回答
舉報