我有一个表单,我希望两个输入字段“CVR number”和“Telephone”仅在值为“Yes”时启用。 (作为最终结果的附件)。表单现在看起来像这样:Example on my form
我尝试使用下面的代码制作一些东西,但我不确定我是否在正确的轨道上?当我使用yes时,第二个输入字段没有激活。
您怎么看?
HTML
<form>
<select id="mySelect" onChange="check(this);">
<option>No</option>
<option>Yes</option>
</select>
<input type="text" id="company" disabled="disabled" placeholder="CVR number">
<input type="text" id="company" disabled="disabled" placeholder="Telephone">
</form>
JS
function check(elem) {
document.getElementById('company').disabled = !elem.selectedIndex;
}
更新了HTML代码
<h3>Do you want a company account?</h3>
<div class="form-group">
<select class="form-control" id="mce-COMPANY" name="COMPANY" id="mySelect" onChange="check(this);">
<option value="Nej">Nej</option>
<option value="Ja">Ja</option>
</select>
</div>
<input placeholder="CVR nummer" name="EMAIL" class="mail" type="text" id="mce-EMAIL" id="company1" disabled="disabled"></input>
<span class="icon4"><i class="fa fa-envelope" aria-hidden="true"></i></span>
<input placeholder="Telefonnummer" name="EMAIL" class="mail" type="text" id="mce-EMAIL" id="company2" disabled="disabled" ></input>
<span class="icon4"><i class="fa fa-envelope" aria-hidden="true"></i></span>
function check(elem) {
document.getElementById('company').disabled = !elem.selectedIndex;
}
<form>
<select id="mySelect" onChange="check(this);">
<option>No</option>
<option>Yes</option>
</select>
<input type="text" id="company" disabled="disabled" placeholder="CVR number">
<input type="text" id="company" disabled="disabled" placeholder="Telephone">
</form>
答案 0 :(得分:1)
您对这两个字段使用了相同的ID。
使用2种不同的ID并且它可以工作..
function check(elem) {
document.getElementById('company1').disabled = !elem.selectedIndex;
document.getElementById('company2').disabled = !elem.selectedIndex;
}
&#13;
<form>
<select id="mySelect" onChange="check(this);">
<option>No</option>
<option>Yes</option>
</select>
<input type="text" id="company1" disabled="disabled" placeholder="CVR number">
<input type="text" id="company2" disabled="disabled" placeholder="Telephone">
</form>
&#13;