我试图:after
在input
字段上使用CSS伪元素,但它不起作用.如果我使用它span
,它可以正常工作.
<style type="text/css">
.mystyle:after {content:url(smiley.gif);}
.mystyle {color:red;}
</style>
Run Code Online (Sandbox Code Playgroud)
这是有效的(在"buu!"之后和"更多"之前放置笑脸)
<span class="mystyle">buuu!</span>a some more
Run Code Online (Sandbox Code Playgroud)
这不起作用 - 它只用红色调色someValue,但没有笑脸.
<input class="mystyle" type="text" value="someValue">
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我应该使用另一个伪选择器吗?
注意:我不能添加span
我的input
,因为它是由第三方控件生成的.
我想知道是否可以在html <input type="date"></input>
标签中设置日期格式...目前它是yyyy-mm-dd,而我需要它以dd-mm-yyyy格式.
我刚刚将ASP.Net MVC应用程序升级到MVC-4.基于DateTime值的输入的字段编辑器现在包括HTML-5 type="date"
属性/值声明.
现在,在Chrome中查看时,我的日期输入在输入字段中显示为"mm/dd/yyyy":
即使我使用value属性传递正确格式的日期:
<input value="2012/10/02" type="date"/>
Run Code Online (Sandbox Code Playgroud)
我仍然在输入框中输入"mm/dd/yyyy",直到用户手动更改值.
问题似乎与Chrome有关,并且与我的后端框架无关.在操作中看到这个问题:jsFiddle
...当然,编辑记录的大问题.如果用户提取已有有效日期的记录,则不会在提交时通过验证,除非他/她单击该字段并手动重置该值.
没有其他浏览器的问题.
这是Chrome错误吗?或者我错过了关于HTML-5日期字段应该如何工作的方式?
更新
请参阅此修订小提琴:http://jsfiddle.net/HudMe/5/ 它同时具有HTML-4和HTML-5日期输入,每个输入都将"10/01/2012"设置为页面加载时的值.
单击任一日期字段.Javascript应该使用该元素的字段值来发出警报.
由于已使用value属性传递了有效日期,因此应显示"10/01/2012",但在Chrome中,对于HTML-5日期字段,不会显示任何内容.手动重置此值,然后再次单击,现在将显示.
HTML5字段中的值按预期显示和警告,无需在Safari,Firefox,IE和Opera中的页面加载后进行调整.
关于已接受答案的注意事项:
对于Asp.net mvc-4的其他用户,您可以使用[DisplayFormat]
视图模型中DateTime字段声明中的属性调整显示格式.(见/sf/answers/884412931/)
我知道在撰写本文时,Opera只支持浏览器UI
<input type="date" name="mydate">
Run Code Online (Sandbox Code Playgroud)
也许我尝试本地化这个领域已经遭遇挫折,因为本地化等细节尚未包含在他们的实现中,但我甚至没有在HTML5规范中看到它.有没有办法指定本地化?我应该在父元素上执行lang ="fr"吗?
关于有关网站实施的一些注意事项:
尝试设置表单但由于某种原因,尽管使用[(ngModel)],但我的html中的Date输入并未绑定到对象的日期值
HTML:
<input type='date' #myDate [(ngModel)]='demoUser.date'/><br>
Run Code Online (Sandbox Code Playgroud)
表单组件:
export class FormComponent {
demoUser = new User(0, '', '', '', '', new Date(), '', 0, [], []);
}
Run Code Online (Sandbox Code Playgroud)
用户类:
export class User {
constructor (
public id: number,
public email: string,
public password: string,
public firstName: string,
public lastName: string,
public date: Date,
public gender: string,
public weight: number,
public dietRestrictions: string[],
public fitnessGoals: string[]
){
}
}
Run Code Online (Sandbox Code Playgroud)
测试输出显示当前"新"Date作为对象的值,但输入不更新User对象的日期值或反映该值,表明这两个双向绑定都不起作用.非常感谢帮助.
我有奇怪的问题.我的日期验证在Chrome中不起作用.我试过这个答案,但它对我不起作用.
我的模型中有这个:
[Display(Name = "Date")]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
Run Code Online (Sandbox Code Playgroud)
我的看法:
<div class="editor-field">
@Html.TextBoxFor(model => model.Item.Date, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.Item.Date)
</div>
$(document).ready(function () {
$('.picker').datepicker({
dateFormat: 'dd.mm.yy',
changeMonth: true,
changeYear: true,
selectOtherMonths: true
});
});
Run Code Online (Sandbox Code Playgroud)
一切都适用于Opera和Firefox,但Chrome不喜欢这种类型的日期.我经常收到以下错误The field 'Date' must be a date
.
有任何想法吗?
UPDATE
当代码在局部视图中时,似乎存在问题.当我将该代码复制到主页时,验证工作正常.
我正在我的主视图中插入局部视图,就像这样:
@Html.Partial("_CreateOrEdit", Model)
Run Code Online (Sandbox Code Playgroud)
上面的代码在局部视图中:
@model Pro.Web.Models.Model
<div class="editor-field">
@Html.TextBoxFor(model => model.Item.Date, new { @class = "picker" })
@Html.ValidationMessageFor(model => model.Item.Date)
Run Code Online (Sandbox Code Playgroud)
$(document).ready(function () { …
Run Code Online (Sandbox Code Playgroud) 在开始之前,我正在使用带有打字稿的Angular2 rc4.如果你想知道,我还不能升级,所以我必须在rc4的约束下完成这项工作.另外我是一个ng2 noob,所以仍然围绕概念和"候选版本"之间的所有变化.
在对值访问器进行了大量研究后,我决定专门创建一个<input type=date />
以避免在组件中需要单独的mule属性,只是为了将日期转换为输入的字符串.
我找到了这个要点,这似乎与我想做的几乎相同.
使用它作为一个起点,几乎结束点,我想出了这个:
import { Directive, ElementRef, Renderer, forwardRef } from "@angular/core";
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from "@angular/forms";
import { isBlank, isDate } from "@angular/forms/src/facade/lang";
import moment from "moment";
export const DATE_VALUE_ACCESSOR: any = {
provide: NG_VALUE_ACCESSOR,
useExisting: forwardRef(() => DateValueAccessor),
multi: true
};
@Directive({
selector:
"input[type=date][formControlName],input[type=date][formControl],input[type=date][ngModel]",
host: {
"(change)": "onChange($event.target.value)",
"(input)": "onChange($event.target.value)",
"(blur)": "onTouched()"
},
providers: [DATE_VALUE_ACCESSOR]
})
export class DateValueAccessor implements ControlValueAccessor {
onChange = …
Run Code Online (Sandbox Code Playgroud) 当我在编辑视图中使用以下代码时,它不会显示模型中的日期.
<input asp-for="OprettetDato" class="form-control"/>
Run Code Online (Sandbox Code Playgroud)
模型的属性声明如下:
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0: dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime OprettetDato { get; set; }
Run Code Online (Sandbox Code Playgroud)
并且值在模型中传递
为什么这不起作用?
我正在尝试处理日期值的用户输入,我想提示用户以这种格式输入日期:dd/MM/yyyy
我试图做的:
我在这个问题中阅读并实现了Darin的答案: 在asp.net mvc 4中格式化日期时间
这是我的实施:
在Global.asax中
(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var displayFormat = bindingContext.ModelMetadata.DisplayFormatString;
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (!string.IsNullOrEmpty(displayFormat) && value != null)
{
DateTime date;
displayFormat = displayFormat.Replace
("{0:", string.Empty).Replace("}", string.Empty);
if (DateTime.TryParse(value.AttemptedValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
else
{
bindingContext.ModelState.AddModelError(
bindingContext.ModelName,
string.Format("{0} is an invalid date format", value.AttemptedValue)
);
}
}
return base.BindModel(controllerContext, bindingContext);
}
Run Code Online (Sandbox Code Playgroud)
模型中的出发日期:
[Required(ErrorMessage = "Departure date is required")]
[Display(Name = "Departure Date")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode …
Run Code Online (Sandbox Code Playgroud) 我有一个经典的 HTML 日期输入:<input type="date" name="dto" id="date_timepicker_end">
现在我需要将此格式更改为dd/mm/yyyy,并且我知道我无法在 html 中更改它。
当我添加 jQuery 日期选择器时,我只得到一个空白输入表单,您可以在其中输入任何您想要的数字:
我需要输入像 HTML input 一样,用户单击输入,并且可以根据他单击的内容更改值。我不希望他能够写出任何随机数。
另请注意,这是 WordPress 主题中的所有自定义代码,因此我有 jquery 以及自定义的 javascript 和 css。我无法添加像 moment.js 之类的库......
这个问题的最佳解决方案是什么?我知道这个问题已经被问过很多次了,但是这些方法都不起作用,因为我需要输入字段像普通的 HTML 输入日期字段一样,而不是空输入,如果 mm/dd 则为 dd/mm/yyyy /yyyy。
jsfiddle: https: //jsfiddle.net/t4zrrvgL/
date ×3
html ×3
angular ×2
asp.net-mvc ×2
c# ×2
datepicker ×2
html5 ×2
jquery ×2
typescript ×2
asp.net ×1
asp.net-core ×1
css ×1
css-content ×1
datetime ×1
format ×1
forms ×1
input ×1
javascript ×1