gpt4 book ai didi

jquery - 从模态中检索 html 并通过脚本将其转换为字符串

转载 作者:行者123 更新时间:2023-12-01 03:11:29 25 4
gpt4 key购买 nike

如何通过脚本检索模式主体中的 html?因为最好将 html 转换为字符串,然后将该内容传递给 Controller ​​(并最终传递给操作方法)。

我以为我可以执行以下操作,但只是收到一条 未定义 消息。

$("#sendButton").click(function () {
alert("Send button pressed");
$.ajax({
url: '@Url.Action("DidItWork", "Home", new { testEmail = "newtestemail@gmail.com"})',
success: function (result) {
var body = $('#myModal').attr('modal-body');
alert("Success. It worked: " + body);
},

关于如何最好地将 html 元素转换为字符串以便它可以与 @Url.Action 中的 testEmail 字符串一起传递的想法/建议?我还应该使用 $('#myModal').data('modal-body') 而不是 .attr 吗?

在 _layout.cshtml 文件中,代码如下:

<div class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title">Modal title</h4>
</div>
<div class="modal-body">
<p>One fine body&hellip;</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->

再往下有一个脚本,其中包含:

<script>
$("#sendButton").click(function () {
alert("Send button pressed");
$.ajax({
url: '@Url.Action("DidItWork", "Home", new { testEmail = "newtestemail@gmail.com"})',
success: function (result) {
alert("Success. It worked: " + result);
},
error: function( xhr, status, errorThrown ) {
alert("Sorry, there was a problem! Error: " + errorThrown + ". Status: " + status + ". Console: " + xhr);
console.log("Hello");
console.log("Error: " + errorThrown);
console.log("Status: " + status);
console.dir(xhr);
console.log("Good-bye");
},
});
return false;
});
</script>

它将 testEmail 变量传递给 homecontroller.cs:

public async Task<ActionResult> DidItWork(string testEmail)
{
var dash = "dash equals nash";

var manager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
var user = new ApplicationUser { UserName = "test2 username2", Email = "testemail2@gmail.com" };
user.Email = testEmail;
var result = await manager.CreateAsync(user, "passwordGoesHere1!");
if (result.Succeeded)
{
await manager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account");

return View();
}

//AddErrors(result);
return View();
}

最佳答案

使用 jQuery .html()函数获取特定元素的 HTML String 内容。

$('#myModal .modal-body').html();

您想要获取 modal-body 类元素的内容。在您的版本中,您可以在 #myModal 元素上获取名为 modal-body 的属性值(该属性返回 undefined,因为不存在此类属性) )

关于jquery - 从模态中检索 html 并通过脚本将其转换为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28179132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com