简体   繁体   English

剃刀和JavaScript编码

[英]Coding Razor and JavaScript

I have a ASP.NET MVC application and question is, how to correctly code what I have done in Razor with JavaScript. 我有一个ASP.NET MVC应用程序,问题是如何正确编码我在JavaScript中使用Razor完成的工作。 In the below example I have three buttons in Razor and the script works correctly. 在下面的示例中,我在Razor中具有三个按钮,脚本可以正常工作。 The in next example I use a helper in JavaScript that needs to do the same, (the script within the script) is not working. 在下一个示例中,我在JavaScript中使用了需要执行相同操作的帮助程序(脚本中的脚本)无法正常工作。 It also appears the bootstrap layout containers are recognized. 它也似乎可以识别引导布局容器。 How do I do this correctly? 如何正确执行此操作?

Razor (working well) 剃刀(效果很好)

panelbar.Add().Text("Third Person")

          .Content(@<div style="padding: 10px;">
              <div id="cont5_container" class="container">

                          <span class="label label-primary">Age</span>
                          <br />
                          <br />
                          <div class="btn-group" id=ageID2>
                      <button type="button" style="width:120px" class="btnMyAge5 btn-default" id="3">Under 10</button>
                      <button type="button" style="width:120px" class="btnMyAge5 btn-default" id="1">Under 50</button>
                      <button type="button" style="width:120px" class="btnMyAge5 btn-default" id="2">Over 50</button>

                      @Html.TextBoxFor(m => m.ageID, new { @class = "input k-textbox", id = "MyAge5", Value = "", style = "width: 50px;" })
                  </div>

                  <script>
                      $(".btn-group > .btnMyAge5").click(function () {
                          $(this).addClass("active").siblings().removeClass("active");
                          document.getElementById('MyAge5').value = $(this).attr("id");
                      })
                  </script>
               </div>

           </div>);

JavaScript (Can't be correct) JavaScript(不正确)

<script type="text/javascript">

function OnClickAdd() {
    $("#panelbar").kendoPanelBar();
    var panelBar = $("#panelbar").data("kendoPanelBar");

    panelBar.append(
        {
            text: "New Person",
            encoded: true,
            content: [

                         '<div id="cont6_container" class="container">',
                                '<span class="label label-primary">Age</span>',
                                '<br /><br />',
                                  '<div class="btn-group" id=ageID>',
                                     '<button type="button" style="width:120px" class="btnMyAge3 btn-default" id="3">Under 10</button><button type="button" style="width:120px" class="btnMyAge3 btn-default" id="1">Under 50</button><button type="button" style="width:120px" class="btnMyAge3 btn-default" id="2">Over 50</button>',
                                     '@Html.TextBoxFor(m => m.ageID, new { @class = "input k-textbox", id = "MyAge3", Value = "", style = "width: 50px;" })',
                                 '</div>',
                           '</div>'
                    ]
           }
                  )
                        }

<script>
$(".btn-group > .btnMyAge3").click(function () {
    $(this).addClass("active").siblings().removeClass("active");
    document.getElementById('MyAge3').value = $(this).attr("id");
})

it looks like at the time your click handler assignment in the second script tag fires the content appended by the first script tag hasn't been added to the DOM. 看起来当您在第二个脚本标签中的点击处理程序分配触发了由第一个脚本标签附加的内容时,该内容尚未添加到DOM。 You should register the click handler in the same function that adds the button to the HTML. 您应该在将按钮添加到HTML的同一函数中注册单击处理程序。

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

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