enable disable select / dropdown on radio selection

不想你离开。 提交于 2020-01-25 04:30:30

问题


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.


回答1:


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



回答2:


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>



回答3:


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>


来源:https://stackoverflow.com/questions/37346081/enable-disable-select-dropdown-on-radio-selection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!