如何使用来自@Html.TextBoxFor的用户输入设置@Html.RadioButtonFor的值

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

本文关键字:@Html 输入 设置 RadioButtonFor 的值 用户 TextBoxFor 何使用      更新时间:2023-11-09

我有一把剃须刀@Html.RadioButtonFor,它的值取自@Html.TextBoxFor值:

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

问题是

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

是一个JavaScript代码,在这种情况下是不正确的,但我不知道如何重写相同的代码,所以它是正确的。

Paul,我不得不用另一种方式:从文本框onChange设置单选值。

看看这个:

@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>

收音机onClick仅用于调试。

希望这能帮助

问候