简体   繁体   English

有没有一种方法可以在MVC视图中获取具有相同标签的所有html元素

[英]Is there a way to get all html elements with the same tag in MVC view

in my index.shtml, I want to change the text of all <button> in page after user checking a checkbox. 在我的index.shtml中,我想在用户选中复选框后更改页面中所有<button>的文本。 I tried this in the checkbox onclick method: 我在复选框onclick方法中尝试了此操作:

    @functions{  

          private void Invisibilize()
          {
              List<HtmlButton> buttons = new List<HtmlButton>();
              buttons = this.HtmlControls.OfType<HtmlButton>().ToList();// error
              foreach (HtmlButton button in buttons)
              {
                  button.Style="Color:red";
                  button.InnerTex = "Currently not selectable";
              } //end foreach
          }// end method       
      }

It does not work. 这是行不通的。 Error says the page: 错误提示页面:

does not contain a definition for 'HtmlControls' 不包含“ HtmlControls”的定义

I already imported all the libraries needed 我已经导入了所有需要的库

@using System.Web.UI.HtmlControls;
@using System.Linq;
@using System.Collections;

Does anyone know how to fix this? 有谁知道如何解决这一问题?


HTML markup: HTML标记:

<input id="cb60" type="checkbox" onclick="Invisibilize()" />
    <label for="cb60">Disable the buttons</label>

It's not possible via the way you're trying. 通过您尝试的方式是不可能的。 MVC works differently than Web Forms. MVC的工作方式不同于Web窗体。 By the time you have a fully rendered HTML document, it's already on it's way back to the client. 当您拥有完全渲染的HTML文档时,它已经可以返回客户端了。 You need to use JavaScript to do this. 您需要使用JavaScript来执行此操作。 That's pretty straight-forward though: 不过,这很简单:

var buttons = document.getElementsByTagName('button');
for (var i = 0; i < buttons.length; i++)
{
    buttons[i].innerHTML = 'new button text';
}

Or, if you want to use jQuery: 或者,如果您想使用jQuery:

$('button').each(function () {
    $(this).html('new button text');
});

EDIT 编辑

The function syntax you have is incorrect. 您使用的函数语法不正确。 In fact, I'm not sure where you even got the idea for that syntax. 实际上,我不确定您从何处获得该语法的想法。 In JavaScript a function is declared thus: 在JavaScript中,因此声明了一个函数:

function myAwesomeFunction() {
    ...
}

There's no accessibility keyword on a JavaScript function because JavaScript has no concept of method accessibility; JavaScript函数上没有可访问性关键字,因为JavaScript没有方法可访问性的概念。 everything is public within scope. 一切都在范围内公开。 There's also no return type on a JavaScript function because JavaScript is duck-typed. JavaScript函数上也没有返回类型,因为JavaScript是鸭子类型的。 Also the @functions bit you have needs to go. 还有@functions你需要去。 I'm not sure what that is. 我不确定那是什么。

So, what you should have is something like: 因此,您应该拥有的类似于:

<script>
    function Invisiblize() {
        // code to alter buttons
    }
</script>

Or, you can just directly bind to the event and put the code there, without a separate named function: 或者,您可以直接绑定到事件并将代码放在此处,而无需单独的命名函数:

$('#checkbox').on('click', function () {
    // code to alter buttons
});

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

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