Home Trying to retain selected checkbox/radio buttons value if previously selected
Reply: 1

Trying to retain selected checkbox/radio buttons value if previously selected

HyperPXLZ
1#
HyperPXLZ Published in 2017-11-14 14:27:23Z
@Request.Form["Gender"] == "Male" ? "selected" : ""

Keeps saying I'm missing name attribute.

Hartanto
2#
Hartanto Reply to 2017-11-14 15:21:01Z

You need to save the value of radiobutton to your database. Then create conditional. If radiobutton just 2 options, use if conditional to keep selected radiobutton. And if more than 2 options you can use switch conditional.

This is sample of radiobutton:

<input type="radio" name="gender" id="male" value="Male" /> Male</div>
<input type="radio" name="gender" id="female" value="Female" /> Female</div>
<input type="radio" name="gender" id="other" value="Other" /> Other</div>

The script to keep selected radiobutton:

<script>
    var gender = "@Model.Gender";
    switch (theme) {
        case "Male":
            document.getElementById("male").checked = true;;
            break;
    case "Female":
            document.getElementById("female").checked = true;;
            break;
    case "Other":
            document.getElementById("other").checked = true;;
            break;
    }
</script>

"Model.Gender" is the way used to call the value stored in the database.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.315298 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO