简体   繁体   English

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

[英]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,它从@ Html.TextBoxFor值获取其值:

@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. 是一个JavaScript代码,在这个上下文中,但我不知道如何重写相同的代码,所以它是正确的。

Paul, I had to make it the other way: from the textbox onChange , set the radio value. 保罗,我不得不另辟蹊径:从文本框onChange ,设置无线电值。

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. 无线电onClick仅用于调试。

Hope this helps 希望这可以帮助

Regards 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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