简体   繁体   中英

How to set a value of @Html.RadioButtonFor with the user input from @Html.TextBoxFor?

I have a razor @Html.RadioButtonFor, which takes its value from @Html.TextBoxFor value:

@Html.RadioButtonFor(m => m.Sms, document.getElementById('@(Model.Phone)').value, new { Id = Model.Sms })

The problem is that

document.getElementById('@(Model.Phone)').value

is a JavaScript code, incorect in this context, but I have no idea how to rewrite the same code so it is correct.

Paul, I had to make it the other way: from the textbox onChange , set the radio value.

Check this out:

@model string
<!DOCTYPE html>
<html>
    <head>
        <title>ViewA</title>
        <script type="text/javascript">
            function changeRadioValue(newValue) {
                var radio = document.getElementById('idRadio');
                if (radio != null) {
                    radio.value = newValue;
                }
                else {
                    //do nothing?
                }
            }
        </script>
    </head>
    <body>
        <div>
            @Html.TextBox("theTextBox", Model, new { id = Model, onChange = "changeRadioValue(this.value)" })
            @Html.RadioButton("radio", "Default radio value", new { id = "idRadio", onClick="alert(this.value)" })
        </div>
    </body>
</html>

The radio onClick is only for debugging.

Hope this helps

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM