enable disable select / dropdown on radio selectio

2019-08-01 06:13发布

I am very new to JQuery, please consider me as a novice. I have a PHP form where in I have a Radio button. Based on the which radio is selected I would like to disable the select / dropdown. Below is the scenario:

Radio Button: Daily and Misc Select: Deity Name

If the user selects Misc then Select (Deity Name) should be disabled and vice versa.

I have already imported the JQuery on my page.

Header.php

!-- JQuery 1.12.3 JS -->
<script src="../js/jquery-1.12.3.min.js"></script>

HTML Code

<tr>
<td class="col-md-4"><label class="control-label">Pooja Type</label></td>
<td class="col-md-8" align="center">
<label class='radio-inline'><input type='radio' name='poojaType' value='daily' checked>Daily</label>
<label class='radio-inline'><input type='radio' name='poojaType' value='misc'>Misc</label>
</td>
</tr>
<tr>
<td class="col-md-4"><label class="control-label" for="deity">Deity Name</label></td>
<td class="col-md-8"><select class="form-control" name="deityName"> 

Please advice.

3条回答
甜甜的少女心
2楼-- · 2019-08-01 06:17

Below is the code for JQuery:

<script>
$(':radio[name=poojaType]').change(function () {
var prop = this.value == 'misc';
$('select[name=deityName]').prop({ disabled: prop, required: !prop });
});
</script>
查看更多
3楼-- · 2019-08-01 06:26

Follow the code:

Assume you have two radio buttons as below:

<input type='radio' name='radios' id='rdbDaily' value='Daily' />&nbsp;Daily
<input type='radio' name='radios' id='rdbMisc' value='Misc' />&nbsp;Misc

and one dropdown as below:

<select id='selectordropdown'>
    <option>Deity Name</option>
</select>

you can use below jquery to enable or disable your dropdown by selecting radio buttons:

$('input:radio[name="radios"]').change(function() {
    if ($(this).val()=='Daily') {
        $('#selectordropdown').attr('disabled', false);
    } 
    else if ($(this).val()=='Misc') {
        $('#selectordropdown').attr('disabled', true);
    }
});

or you can also use below jquery to enable / disbale your dropdown:

$("#rdbDaily").click(function() {
    $("#selectordropdown").attr("disabled", false);
});

$("#rdbMisc").click(function() {
    $("#selectordropdown").attr("disabled", true);
});
查看更多
贼婆χ
4楼-- · 2019-08-01 06:29

You can just get the checked value on change event of radio button and set it against the attribute disabled value as below

$('input[name="poojaType"]').on('change', function() {
  $('select[name="deityName"]').attr('disabled', this.value != "daily")
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<tr>
  <td class="col-md-4">
    <label class="control-label">Pooja Type</label>
  </td>
  <td class="col-md-8" align="center">
    <label class='radio-inline'>
      <input type='radio' name='poojaType' value='daily' checked/>Daily</label>
    <label class='radio-inline'>
      <input type='radio' name='poojaType' value='misc' />Misc</label>
    <select name="deityName">
      <option>Value 1</option>
      <option>Value 2</option>
      <option>Value 3</option>
      <option>Value 4</option>
    </select>
  </td>
</tr>

查看更多
登录 后发表回答