我可以在MVC中使用javascript或jquery和razor生成的动态单选按钮吗

我可以在MVC中使用javascript或jquery和razor生成的动态单选按钮吗,javascript,jquery,model-view-controller,Javascript,Jquery,Model View Controller,大家好:我正在尝试根据razor生成的“动态”单选按钮切换div: for (int i = 0; i < Model.Count; i++) { <div class="form-row"> <div class="form-group col-md-2">

大家好:我正在尝试根据razor生成的“动态”单选按钮切换div:

    for (int i = 0; i < Model.Count; i++)
                    {                       
                        <div class="form-row">
                            <div class="form-group col-md-2">
                                <div>@Html.DisplayFor(m => m[i].DrugName)</div>
                            </div>
                            <div class="form-group col-md-3">
                                <label class="form-label">Have you ever sold, smuggled, or transported for sale or personal Gain:</label><br />
                                <label class="custom-control custom-radio custom-control-inline">
                                    @Html.RadioButtonFor(m => m[i].Sold, "true", new { Id = "Sold_" + i, @class = "custom-control-input" })
                                    <span class="custom-control-label">Yes</span>
                                </label>
                                <label class="custom-control custom-radio custom-control-inline">
                                    @Html.RadioButtonFor(m => m[i].Sold, "false", new { Id = "Sold_" + i,  @class = "custom-control-input"})
                                    <span class="custom-control-label">No</span>
                                </label>
                            </div>
                            <div class="form=group col-md-2">
                                <label class="form-label">Have you ever used, possessed or experimented with?</label>
                                <label class="custom-control custom-radio custom-control-inline">
                                    @Html.RadioButtonFor(m => m[i].Used, "true", new { Id = "Used_" + i,  @class = "custom-control-input" })
                                    <span class="custom-control-label">Yes</span>
                                </label>
                                <label class="custom-control custom-radio custom-control-inline">
                                    @Html.RadioButtonFor(m => m[i].Used, "false", new { Id = "Used_" + i,  @class = "custom-control-input"})
                                    <span class="custom-control-label">No</span>
                                </label>
                            </div>
-----OTHER CODE-------REMOVED----------
for(int i=0;im[i].DrugName)
您是否曾经为销售或个人利益而出售、走私或运输:
@RadioButtonOn(m=>m[i].salled,“true”,新的{Id=“salled”+i,@class=“custom control input”}) 对 @RadioButtonOn(m=>m[i].salled,“false”,新的{Id=“salled”+i,@class=“custom control input”}) 不 你曾经使用、拥有或尝试过吗? @RadioButton(m=>m[i].Used,“true”,new{Id=“Used”+i,@class=“custom control input”}) 对 @RadioButton(m=>m[i]。使用了“false”,新的{Id=“Used”+i,@class=“custom control input”}) 不 -----其他代码------已删除----------
使用:

   var drugUseCount = document.getElementById("DrugUseCount").innerHTML;
  for (var i = 0; i < drugUseCount; i++) {
    $("input:radio[name='\\[0\\].Used']").click(function() {
      if ($(this).val() === true) {
        alert("True");
      } else if ($(this).val() === false) {
        alert("False");
      }
    });
  }
});
var drugUseCount=document.getElementById(“drugUseCount”).innerHTML;
对于(变量i=0;i

我知道我应该使用“I”,但实际上我只是试图捕捉第一个项目的点击或更改事件。它似乎忽略了这一事件。我把自己弄得一团糟,需要一些指导。任何帮助都将不胜感激。我对此束手无策

选项1:在razors html助手中使用三元运算符向目标元素添加一个类,然后在类名上绑定click元素。如果您的需求在将来变得更加复杂,则此路线可能更具可扩展性:

    <div class="form=group col-md-2">
        <label class="form-label">Have you ever used, possessed or experimented with?</label>
        <label class="custom-control custom-radio custom-control-inline">
            @Html.RadioButtonFor(m => m[i].Used, "true", i == 0 ? new { Id = "Used_" + i, @class = "custom-control-input drugUsed" } : new { Id = "Used_" + i, @class = "custom-control-input" })
            <span class="custom-control-label">Yes</span>
        </label>
        <label class="custom-control custom-radio custom-control-inline">
            @Html.RadioButtonFor(m => m[i].Used, "false", i == 0 ? new { Id = "Used_" + i, @class = "custom-control-input drugUsed" } : new { Id = "Used_" + i, @class = "custom-control-input" })
            <span class="custom-control-label">No</span>
        </label>
    </div>


    $(".drugUsed").click(function () {
        var eleId = $(this).attr('id');
        if ($(this).val() == "true") {
            alert('true for element ' + eleId);
        } else {
            alert('false for element ' + eleId);
        }
    });

这是假设您的js文件或脚本位于razor语法之后,或者您将其封装在document.ready函数中。

选项1:使用razors html帮助程序中的三元运算符将类添加到目标元素,然后在类名上绑定click元素。如果您的需求在将来变得更加复杂,则此路线可能更具可扩展性:

    <div class="form=group col-md-2">
        <label class="form-label">Have you ever used, possessed or experimented with?</label>
        <label class="custom-control custom-radio custom-control-inline">
            @Html.RadioButtonFor(m => m[i].Used, "true", i == 0 ? new { Id = "Used_" + i, @class = "custom-control-input drugUsed" } : new { Id = "Used_" + i, @class = "custom-control-input" })
            <span class="custom-control-label">Yes</span>
        </label>
        <label class="custom-control custom-radio custom-control-inline">
            @Html.RadioButtonFor(m => m[i].Used, "false", i == 0 ? new { Id = "Used_" + i, @class = "custom-control-input drugUsed" } : new { Id = "Used_" + i, @class = "custom-control-input" })
            <span class="custom-control-label">No</span>
        </label>
    </div>


    $(".drugUsed").click(function () {
        var eleId = $(this).attr('id');
        if ($(this).val() == "true") {
            alert('true for element ' + eleId);
        } else {
            alert('false for element ' + eleId);
        }
    });

这是假设您的js文件或脚本位于razor语法之后,或者您将其封装在document.ready函数中。

由于您使用了Jquery,“This.value”将无法工作,因为它无法识别为有效函数。请尝试使用:$(this).val()==true,如果部分为else if,则使用相同的值。好的,谢谢,我将尝试一下。我很抱歉,为了让这一切顺利进行,我一直在为各种各样的尝试而疯狂。现在的代码太草率了!没问题。试试这个,我希望这会有帮助。这并没有解决它。我已经设置了断点,就像找不到元素一样。我在chrome的调试窗口中没有看到任何错误。这一行我认为是问题所在:$(“输入:radio[name=\[0\].Used'])。单击(function(){我明白了。让我们逐行分析它。DrugUseCount从哪里来?我认为它在您刚才提供的视图中不可见。因为您使用了Jquery,'This.value'将无法工作,因为它无法识别为有效函数。请尝试使用:$(This).val()===正确,其他if部分也一样。好的,谢谢,我会尝试一下。我很抱歉,我在很多事情上都尝试了不同的尝试来让它工作。目前的代码很邋遢!没问题。尝试一下我希望这会有帮助。但这并没有解决问题。我已经设置了断点,好像找不到元素。我没有看到任何错误chrome的调试窗口。我认为这一行是问题所在:$(“输入:radio[name='\[0\].Used'])。单击(function()){我明白了。让我们逐行分析它。DrugUseCount来自哪里?我想它在您刚才提供的视图中不可见。是的,先生!就这样。您是一位绅士和学者。我现在正在使用选项1,只是添加类名。选项2我需要再试一次。这个选项感觉正确,但我认为我无法让它工作在[0]将位于变量索引[i]中的循环中动态初始化。使用染料,先生!这样就可以了。您是一位绅士和学者。我现在只通过添加类名来使用选项1。选项2我需要重试。该选项感觉正确,但我认为无法在[0]中的循环中动态运行它将位于变量索引[i]中。已使用