答案 0 :(得分:1)
您只需要与===而不是=
进行比较
function onChange(fromOrTo) {
if (fromOrTo === 'from') {
document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
} else if (fromOrTo === 'to') {
document.getElementById("to").placeholder = document.getElementById("selectTo").value;
}
}
<body>
<div id="sect">
<section>
<select onchange="onChange('from')" id="selectFrom">
<option value="Celcius">Celcius</option>
<option value="Farenheit">Farenheit</option>
<option value="Kelvin">Kelvin</option>
<option value="Reamur">Reamur</option>
</select>
<span class="toText">to</span>
<select onchange="onChange('to')" id="selectTo">
<option value="Celcius">Celcius</option>
<option value="Farenheit">Farenheit</option>
<option value="Kelvin">Kelvin</option>
<option value="Reamur">Reamur</option>
</select>
</section>
<section>
<input type="text" id="from">
<span id="toText">to</span>
<input type="text" id="to">
</section>
</div>
</body>
答案 1 :(得分:0)
每个if
条件中只有一个等号。这是变量的assigns而不是compares。切换到两个等号(或三个以避免类型强制)将解决此问题:
function onChange(fromOrTo) {
if (fromOrTo === 'from') {
document.getElementById("from").placeholder = document.getElementById("selectFrom").value;
} else if (fromOrTo === 'to') {
document.getElementById("to").placeholder = document.getElementById("selectTo").value;
}
}
<body>
<div id="sect">
<section>
<select onchange="onChange('from')" id="selectFrom">
<option value="Celcius">Celcius</option>
<option value="Farenheit">Farenheit</option>
<option value="Kelvin">Kelvin</option>
<option value="Reamur">Reamur</option>
</select>
<span class="toText">to</span>
<select onchange="onChange('to')" id="selectTo">
<option value="Celcius">Celcius</option>
<option value="Farenheit">Farenheit</option>
<option value="Kelvin">Kelvin</option>
<option value="Reamur">Reamur</option>
</select>
</section>
<section>
<input type="text" id="from">
<span id="toText">to</span>
<input type="text" id="to">
</section>
</div>
</body>