【发布时间】:2015-07-06 19:48:22
【问题描述】:
我被难住了。我正在尝试检查我的输入类型:文本在我的 JavaScript 中是否为空或 null。我目前有这个:
if ($("#startDate").val().trim().length() === 0)
{
alert("Please fill out the required fields.");
}
我试过了:.val() === "",.val() == "",.length() === 0,.length() == 0,.length() < 1,.val().length() === 0,val().trim().length() < 1,.trim().length() < 1,.text() === "",@98765433@4,
我的 HTML 是:
<fieldset>
<label for="startDate_[pk]" style="display:block; width:100%;text-align:left">Start Time</label>
<input type="text" name="startDate_[pk]" id="startDate_[pk]" style="width:75px;display:inline" placeholder="pick date" />
@@
<select name="startTime_[pk]" id="startTime_[pk]" style="display:inline;" disabled="disabled">
@{
var time = DateTime.Now.Date.AddHours(8);
for (int i = 480; i < 1260; i += 15)
{
<option value="@i">@DateTime.Now.Date.AddMinutes(i).ToShortTimeString()</option>
}
}
</select>
</fieldset>
我也试过这个:
$('input[class^="dateValidate"]').each(function () {
if($(this).val().trim().length === 0)
{
alert("Please fill out the required fields.");
}
我的想法已经不多了。
****更新****
验证适用于一个选定的对象。但是当您选择多个时,验证仅适用于第一个对象。这是我的代码:
function validate()
{
var startDate = $('[id^="startDate"]').filter(function(){
return $(this).val().length!==0;
});
var showingType = $('[id^="tShowingType"]').filter(function () {
return $(this).val() === "";
});
var startTime = $('[id^="startTime"]').filter(function () {
return $(this).val() === "";
});
var endTime = $('[id^="endTime"]').filter(function () {
return $(this).val() === "";
});
if (startDate.length == 0 || showingType.val() === "" || startTime.val() === "" || endTime.val() === "")
{
alert("Please fill out the required fields.");
}
else
{
UI.goToConfirmStep();
}
我试过了:
var startDate = $('[id^="startDate"]').filter(function(){
return $(this).val().length!==0
var startDate = $('[id^="startDate"]').filter(function(){
return $(this).val().length===0
startDate.length == 0
startDate.length < 0
startDate.length < 1
【问题讨论】:
-
你也可以发布 HTML 吗?
-
你试过的那个有什么问题?他们不工作吗?
-
他们没有触发警报。它只是失败而没有错误。
-
试试这个
if($("#startDate").val().trim())这个条件应该处理假字符串(空或未定义或空) -
一件事:
$("#startDate").val().trim().length === 0,长度不是函数,而是属性。但我想这是错字,因为你说没有错误。
标签: javascript jquery validation input