[英]Adding styling to non-input elements during jquery validation
I am using Jquery validation and a Tag-it plugin that generates UL LIs on the fly for each new tag to be added. 我正在使用Jquery验证和Tag-it插件,该插件会为要添加的每个新标签动态生成UL LI。 If there are no tags, the plugin has only one LI and it is empty.
如果没有标签,则插件只有一个LI,并且为空。 If you create at least one tag, then the plugin creates a second LI.
如果您创建至少一个标签,那么插件将创建第二个LI。 I am using submitHandler and invalidHandler to validate only if there is more than 1 LI, which means there is at least one tag.
我仅使用1个以上的LI来使用SubmitHandler和invalidHandler进行验证,这意味着至少有一个标签。 This works.
这可行。 Here is my fiddle: http://jsfiddle.net/Z3HDh/95/
这是我的小提琴: http : //jsfiddle.net/Z3HDh/95/
I need help with two additional things: 我需要另外两点帮助:
When the form fails to validate because no tags have been entered, it writes to the console - "you don't have any tags". 当表单由于未输入标签而无法通过验证时,它将写入控制台-“您没有任何标签”。 It also displays a message, "Add some tags, puhleeeease!"
它还显示一条消息,“添加一些标签,puhleeeease!” I want it to add the error styling to the background and border of the ul class="tagit ui-widget ui-widget-content ui-corner-all".
我希望它向ul class =“ tagit ui-widget ui-widget-content ui-corner-all”的背景和边框添加错误样式。 I have unsuccessfully tried using jquery validation's highlight and addrules functions.
我尝试使用jquery验证的Highlight和addrules函数未成功。
If the field fails to validate (there is no tag, the "Add some tags, puhleeeease!" message appears), and then I put in a tag (which means the field now validates), it will only register the validation and remove the error message if I click submit again. 如果该字段未能通过验证(没有标签,则显示“添加一些标签,puhleeeease!”消息),然后我放入一个标签(这意味着该字段现在已验证),它将仅注册验证并删除错误消息,如果我再次单击提交。 On other fields, the plugin itself validates on the fly and adjusts the styling.
在其他领域,插件本身可以即时验证并调整样式。 So, in this case, as soon as I add the tag, I would like the field to validate successfully and remove the error styling and message, rather than waiting until I re-submit it.
因此,在这种情况下,我一旦添加标签,就希望该字段成功验证并删除错误样式和消息,而不是等到我重新提交它。 I have seen a number of examples of this but also have failed to implement it.
我已经看到了许多这样的示例,但是也没有实现它。
HTML: HTML:
<div class="form-group">
<label for="id_title">Add your title here (required)</label>
<input type="text" id="id_title" name="title" maxlength="75" value=""
required data-msg="Please add a title.">
</div><!-- end form-group -->
<div class="form-group">
<label for="id_material">Your tags go here... (required)</label>
<input type="text" id="id_material" name="material" value=""
required data-msg="Add some tags, puhleeeease!">
</div>
<div class="form-group-buttons">
<p class="form_buttons">
<a href="<?php echo $this->cancel_link . $this->item_type; ?>s" class="dgrey-button cancel-button">Cancel</a>
<button type="submit">Submit</button>
</p>
</div><!-- end form-group-buttons -->
</form>
JS: JS:
$("#id_material").tagit();
$("#add_idea_form").validate({
ignore: "input[name='tags']:hidden",
submitHandler: function (form) {
},
invalidHandler: function(event, validator) {
if($('#id_material + ul').length < 2){
console.log("You don't have any tags");
// return false;
}
}
});
$("li.email > input").rules("add", {
required: true,
email: true
});
CSS: CSS:
input.error, td.field input.error, td.field select.error, tr.errorRow td.field, input, tr.errorRow td.field select {
/*background-color: #ffffd5;*/
border:2px solid #d17877;
background: #F2DEDE;
box-shadow: none;
}
#add_idea_form label.error{
color: red;
}
You need to add a delete rule when a tag is entered. 输入标签时,您需要添加删除规则。 The example below will remove the "Add tags please" when a tag is added (after the error was run).
下面的示例将在添加标签时(运行错误后)删除“请添加标签”。
$("#id_material").tagit({
afterTagAdded: function(event, ui) {
$("#id_material-error").remove();
$(".ui-corner-all").removeClass('error');
console.log(ui.tag);
}
});
To add CSS around the tag area, add the following code to your invalid Handler 要在标签区域周围添加CSS,请将以下代码添加到无效的处理程序中
invalidHandler: function(event, validator) {
if($('#id_material + ul').length < 2){
console.log("You don't have any tags");
$('.ui-corner-all').addClass('error');
}
}
}); });
It will add a red outline inside the tag input box. 它将在标签输入框中添加一个红色轮廓。 If you have multiple tag input boxes, the code may need to be updated to target a specific input.
如果您有多个标签输入框,则可能需要更新代码以针对特定输入。
You'll also need to add the following to your css file 您还需要将以下内容添加到CSS文件中
.error{
border:2px solid #d17877;
background: #F2DEDE;
box-shadow: none;
}
.ui-corner-all.error{
background:red;
}
Working example at http://jsfiddle.net/v0jdpkz8/8/ http://jsfiddle.net/v0jdpkz8/8/上的工作示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.