gpt4 book ai didi

jquery - 伪类 :empty and :not(:empty) - make it hide() and show() regarding content (jquery, mvc 5, asp.net, css)

转载 作者:行者123 更新时间:2023-11-28 10:28:40 24 4
gpt4 key购买 nike

我遇到了 :empty 和 :not(:empty)(jquery, mvc 5, asp.net, css) 的问题。我的目标是仅在出现“需要街道字段”之类的文本时才显示错误消息。否则让它消失。它仅部分起作用。

在服务器端它看起来像:

<div class="BWForm_form-group">
<div class="col-md-4 BWTextLeft">
@Html.BWLabelFor(model => model.street, new { @class = "BWLabel" })
<b>/</b>
@Html.BWLabelFor(model => model.streetNo, new { @class = "BWLabel" })
</div>
<div class="col-md-8">
<div class="col-md-9 BWPaddingRight">
@Html.EditorFor(model => model.street, new { @class = "BWForm_form-control BWTextBox" })
</div>
<div class="col-md-3 BWPaddingLeft">
@Html.EditorFor(model => model.streetNo, new { @class = "BWForm_form-control BWTextBox" })
</div>
</div>
<div class="clearfix"></div>
<div class="col-md-offset-4 col-md-8">

@Html.ValidationMessageFor(model => model.street)
@Html.ValidationMessageFor(model => model.streetNo)

</div>
</div>

我的问题的相关部分只是“@Html.ValidationMessageFor(model => model.street)”,这将导致原始 HTML 看起来像(只有相关代码):

<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="receiverAddress.street"></span>

如果发生错误(发送表单和检测错误时),将显示以下 HTML(之前的(“原始 HTML”)html 将被完全替换):

<span class="field-validation-error" data-valmsg-replace="true" data-valmsg-for="receiverAddress.street">
<span class="" for="receiverAddress_street">The Street field is required.</span>
</span

我的想法是这样做(它有效):

 $('span.field-validation-error:empty').hide();
$('span.field-validation-valid:empty').hide();

但我想再次重新显示它(这不起作用):

$('span.field-validation-error:not(:empty)').show(100);
$('span.field-validation-valid:not(:empty)').show(100);

Html 看起来仍然像(style="display: none;"它仍然被禁用):

<span class="field-validation-error" data-valmsg-replace="true" data-valmsg-for="receiverAddress.street" style="display: none;">

<span class="" for="receiverAddress_street">

The Street field is required.

</span>

</span>

但是我该怎么做才能再次显示它,为什么它不起作用?

感谢您的帮助。马丁

(更新:添加了附加信息)

最佳答案

试试这个(模式)

已更新

$(function () {
$("span[class^=field-validation]")
.each(function (i, el) {
if ( $(el).text().length > 1 ) {
$(el).css("display", "block")
};
});
});

jsfiddle http://jsfiddle.net/guest271314/tF2YJ/

关于jquery - 伪类 :empty and :not(:empty) - make it hide() and show() regarding content (jquery, mvc 5, asp.net, css),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23613170/

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