gpt4 book ai didi

javascript - 在条件下使用 Javascript 禁用下拉框

转载 作者:可可西里 更新时间:2023-11-01 13:06:02 27 4
gpt4 key购买 nike

我有一个表单,其中有几个单选按钮和一个下拉框。

我想要什么?

  1. 当我选择“收据”或“付款”单选按钮时,下拉框必须处于事件状态。
  2. 当我选择“其他收入”或“其他费用”单选按钮时,必须禁用下拉框。

下面提到了我尝试过的 HTML 和 JS。任何帮助将不胜感激。

HTML

<label class="radio-inline"><input type="radio" name="type" id="type" value="receipt" onClick="party_check()" />Receipt</label>

<label class="radio-inline"><input type="radio" name="type" id="type" value="payment" onClick="party_check()" />Payment</label>

<label class="radio-inline"><input type="radio" name="type" id="type" value="other income" onClick="party_check()" />Other Income</label>

<label class="radio-inline"><input type="radio" name="type" id="type" value="other expense" onClick="party_check()" />Other Expense</label>

<select name="party" id="party" class="form-control">
<option value="">Please select an option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

JS

function party_check(){
if((document.type[0].checked) || (document.type[1].checked)){
document.getElementById("party").disabled=false;
}
else
{
document.getElementById("party").disabled=true;
}
}

最佳答案

检查这个

HTML

<label class="radio-inline"><input type="radio" name="type"  value="receipt" onclick="party_check(this)" />Receipt</label>

<label class="radio-inline"><input type="radio" name="type" value="payment" onclick="party_check(this)" />Payment</label>

<label class="radio-inline"><input type="radio" name="type" value="other income" onclick="party_check(this)" />Other Income</label>
<label class="radio-inline"><input type="radio" name="type" value="other expense" onclick="party_check(this)" />Other Expense
</label>

<select name="party" id="party" class="form-control">
<option value="">Please select an option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

Javascript

function party_check(rad){
if(rad.value == "receipt" || rad.value == "payment"){
document.getElementById("party").disabled=false;
}else{

document.getElementById("party").disabled=true;
}
}

“id”属性的值在 html 页面的标签中必须是唯一的

关于javascript - 在条件下使用 Javascript 禁用下拉框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33067429/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com