0

I'm trying to get the selected item from my dropdown's and use it for validation.

Previously i used:

$("#PolicyOrganisation").text());

Which worked but then I changed my dropdowns's to the below(loaded from database rather than hardcoded):

<div class="form-group">
    <label for="diagnosticMode" class="control-label col-xs-2">Policy Organisation:</label>
    <select id="DD1" name="PolicyOrganisation">
        <option value="-1">Select</option>
        @foreach (var item in ViewBag.PolicyOrgs)
        {
            <option value="@item.Id">@item.Name</option>
        }
    </select>
</div>

And my JQuery Validation too:

 //Validation for Dropdown      
    var platform1 = $('#PolicyOrganisation option:selected').text();
    var Valid1 = (platform1 !== 'Select')
    if (!Valid1) {
        $('#PolicyOrganisation').css({
            "border": "1px solid red",
            "background": "#FFCECE"
        });
    }

I found this method through searching about this question.. Get selected text from a drop-down list (select box) using jQuery

But the new versions don't work? Can anyone suggest why this might be?

1
  • Out of curiosity, why would you not use @Html.DropDownListFor()` and @Html.ValidationMessageFor() with jquery unobtrusive validation? Commented Sep 19, 2014 at 11:37

1 Answer 1

2

'PolicyOrganisation' is not an id it is name attribute Please use 'DD1' instead of 'PolicyOrganisation'

var platform1 = $('#DD1 option:selected').text();
    var Valid1 = (platform1 !== 'Select')
    if (!Valid1) {
        $('#DD1').css({
            "border": "1px solid red",
            "background": "#FFCECE"
        });
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks I had overlooked this. unfortunately its still not working for me tho (my textbox validation works so i know jquery is working)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.