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

时间:2015-10-11 16:35:13

标签: javascript html forms radio-button html-select

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

我想要什么?

  1. 当我选择单选按钮'收据'或'付款'时,下拉框必须处于活动状态。
  2. 当我选择单选按钮'其他收入'或'其他费用'时,必须禁用下拉框。
  3. 我尝试过的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;
        }
    }
    

2 个答案:

答案 0 :(得分:2)

检查

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;
    }
}

&#34; ID&#34;属性的值必须在html页面中的标记中是唯一的

答案 1 :(得分:0)

你可以使用这样的东西:

- 设置ID为**Yes****No**的ID,并使用click function

在JS中执行此操作

$("#Yes").click(function() {
  $("#party").attr("disabled", false);
  //$("#discountselection").show(); //To Show the dropdown
});
$("#No").click(function() {
  $("#party").attr("disabled", true);
  //$("#discountselection").hide();//To hide the dropdown
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="radio-inline"><input type="radio" name="type" id="Yes" value="receipt" onClick="party_check()" />Receipt</label>

<label class="radio-inline"><input type="radio" name="type" id="Yes" value="payment" />Payment</label>

<label class="radio-inline"><input type="radio" name="type" id="No" value="other income"  />Other Income</label>

<label class="radio-inline"><input type="radio" name="type" id="No" value="other expense"  />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>