我是一个JS noob试图让这个"你好mr./miss yourname!" 清洁.我没有看到在if/else中重构警报的方法,因为那时我失去了var b的值.
JS:
<script>
"use strict";
window.onload = function () {
let form1 = document.getElementById('myForm');
form1.addEventListener('submit', helloYou);
function helloYou() {
let x = document.getElementById("1").value;
let a = document.getElementById('3').value;
if ( a === "M") {
let b = "Mr.";
alert('Hello ' + b + " " + x + "!");
}
else {
let b = "Miss";
alert('Hello ' + b + " " + x + "!");
}
}
}
</script>
Run Code Online (Sandbox Code Playgroud)
HTML:
<body>
<form id="myForm">
Write …Run Code Online (Sandbox Code Playgroud)