我目前正在尝试将点击处理程序附加到我的 <a>
标签将弹出我的模态,它有一个动态 ID。我无法弄清楚完成此操作的语法 - 我当前的尝试可以在下面找到,并且在 onclick="$('#"+ @modalId + "')"
上有语法错误(这是我有点挣扎的部分)。我怎样才能做到这一点?
var modalId = string.Format("docusign{0}", doc.Id)
<a href="#" onclick="$('#"+ @modalId + "').modal('toggle')">@string.Format("Document {0}", EnumHelper.GetDisplayName(doc.DocumentType))</a>
<div id="@modalId" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header" style="background-color:#337ab7;color:#ffffff">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">DocuSign Notice</h4>
</div>
<div class="modal-body form-group">
<h5>Please allow up to 10 minutes for us to receive your e-form after you have completed it. To continue and fill out the necessary e-form, please click the "Fill Form" button below</h5>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default pull-right" data-dismiss="modal">Close</button>
<a class="btn btn-default pull-left" href=@doc.DocuSignPowerFormLink>Fill Form</a>
</div>
</div>
</div>
</div>
最佳答案
您当前的代码正在像这样渲染标记(假设doc.Id
的值为123
<a href="#" onclick="$('#" +="" docusign123"" "').modal('toggle')"="">Your text</a>
C# 变量值前后有额外的 "
。以及额外的 +
所以基本上你的 jQuery 选择器是
$('#" +="" docusign1324"" "')
这是不正确的
您所需要的只是渲染 @modalId
的值,并在 jQuery 选择器的 #
部分之后使用该值。
这应该有效
<a href="#" onclick="$('#@modalId').modal('toggle');">Your text</a>
如果你想提高可读性,可以将 C# 表达式包装在 (
和 )
<a href="#" onclick="$('#@(modalId)').modal('toggle');">Your text</a>
关于jquery - MVC Razor动态构建onclick事件以显示模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47290297/