首页 > 解决方案 > ASP.NET - 可以在按钮上创建带有 onclick 事件的标签吗?

问题描述

我有一个带有 TextArea 的页面,可让您发送一条消息。但就我而言,我想发送几条消息。

我想有一个按钮,可以根据需要创建一个新的 TextArea,但我没有找到解决方案。

@Html.TextAreaFor(m => m.MessageToSend, new { style = "width: 1200px; height: 600px;" }) 上面是我想通过单击按钮创建/复制的文本框。

-- 我已经可以发送消息了,我只想通过单击按钮创建其他 TextArea

标签: javascriptasp.netasp.net-mvcasp.net-core

解决方案


是的,你可以,首先你需要安装 jquery。然后,您需要将代码放在 div 中,例如:

 <div id="multiple">
     @Html.TextAreaFor(m => m.MessageToSend, new { style = "width: 1200px; height: 600px;" 
      })
  </div

然后创建一个按钮来添加多个办公桌,例如:-

   <button class="btn btn-primary" onclick="adder()">Adder</button>

在,最后写脚本

  <script>
       function adder()//This function is called when u click that button
        {
           var html='';
           html += '@Html.TextAreaFor(m => m.MessageToSend, new { style = "width: 1200px; height: 600px;" })
          })';
          $("#multiple").append(html); //This will stack your html input inside that id as we have name multiple and given id
      
        }
    
      </script>

但是,您需要管理您的控制器


推荐阅读