Jfabs
Jfabs

Reputation: 563

Enable/Disable form elements based on dropdown list

So I have been looking around at questions on here and I have gotten far enough to be able to disable a textbox by changing the selection of the dropdownlist, but I want to be able to enable it again if the dropdownlist goes back to its default value of <Select an Access Point>.

JQuery:

$('#selectAccessPoint').change(function () {
    if ($('#selectAccessPoint :selected').val != "2147483647")
        $('#newAccessPoint').attr('disabled', 'disabled');
    else {
        $('#newAccessPoint').removeAttr('disabled');
        $('#newAccessPoint').attr('enabled', 'enabled');
    }
});

HTML for textbox and dropdownlist: `

        <tr>
        <td><label for ="AccessPoint" class="xl">Access Point:</label></td>
            <td><%= Html.DropDownListFor(x => x.AccessPointsList.Id, Model.AccessPointsList.AccessPoints.OrderByDescending(x => x.Value.AsDecimal()), new { @id = "selectAccessPoint", @class = "info1"})%></td>
        </tr>
        <tr>
            <td><label for ="AccessPoint" class="xl">Or Add New:</label></td>
            <td><%= Html.TextBoxFor(x => x.AccessPointsList.AccessPoint, new { @id = "newAccessPoint", @class = "location info2 xl", maxlength = "250" }) %></td>
        </tr>

Generated HTML: <select class="info1" data-val="true" data-val-number="The field Id must be a number." data-val-required="The Id field is required." id="selectAccessPoint" name="AccessPointsList.Id"><option value="2147483647">&lt;Select an Access Point&gt;</option> (there are more options in there but this is the one I am comparing against)

<input class="location info2 xl" id="newAccessPoint" maxlength="250" name="AccessPointsList.AccessPoint" type="text" value="">

Notes: attr must be used as prop gives me an error and val() also gives me an error.

Upvotes: 2

Views: 15247

Answers (2)

palaѕн
palaѕн

Reputation: 73896

Using jquery v1.9.1

$('#selectAccessPoint').change(function () {
    if ($(this).find('option:selected').text() != '<Select an Access Point>') {
        $('#newAccessPoint').prop('disabled', true);
    } else {
        $('#newAccessPoint').prop('disabled', false)
    }
});
  • $('#selectAccessPoint:selected') not correct. It should be $('#selectAccessPoint option:selected')
  • .text not correct. It should be .text()
  • To disable a textbox simply use this prop('disabled', true) using the jquery v1.9.1.
  • To enable a textbox simply use this prop('disabled', false).

Using jquery v1.4.4

$('#selectAccessPoint').change(function () {
    if ($(this).find('option:selected').text() != 'Select an Access Point') {
        $('#newAccessPoint').attr('disabled', 'disabled');
    } else {
        $('#newAccessPoint').attr('disabled', '')
    }
});

Upvotes: 7

Josh Balcitis
Josh Balcitis

Reputation: 498

You might want to try something like this

HTML

<select name="foo" id="foo" onChange="javascript:changeTextBoxState(this)">
  <option>Select Something</option>
  <option>FooBar</option>
</select>

<input name="bar" id="bar" type="text" />

jQuery

function changeTextBoxState(dropDown) {

  switch (dropDown.value) {
    case 'Select Something': {
       $('#bar').removeAttr("disabled");
    }
    case 'FooBar': {
       $('#bar').addAttr('disabled', 'disabled');
    }
  }
}

There is no enabled attribute on input tag only disabled.

Hope this helps

Upvotes: 1

Related Questions