我正在检查表单中的每个必填字段.我尝试了这个,但它只适用于第一个输入.如何使其适用于每个输入?
if($('[required]').val() != ''){
$('.button1').fadeIn(0);
$('.button2').fadeOut(0);
}else{
$('.button1').fadeOut(0);
$('.button2').fadeIn(0);
}
Run Code Online (Sandbox Code Playgroud)
编辑:对于更多上下文,这是我在其下面的另一个函数,以便在他们更改和HTML代码时应用.
$('[required]').change(function() {
if($('[required]').val() != ''){
$('.button1').fadeIn(0);
$('.button2').fadeOut(0);
}else{
$('.button1').fadeOut(0);
$('.button2').fadeIn(0);
}
});
Run Code Online (Sandbox Code Playgroud)
HTML
我有很多这样的输入:
<input type="text" id="title" name="title" value="" required>
<input type="text" id="size" name="size" value="" required>
<input type="text" id="length" name="length" value="" required>
Run Code Online (Sandbox Code Playgroud)
纽扣:
<a class="button1" style="display:none" >button 1</a>
<a class="button2">button 2</a>
Run Code Online (Sandbox Code Playgroud)
所以我想在页面加载时以及每当它们发生变化时检查空字段.
顺便说一句,我没有围绕这些的表格标签,因为它是针对移动设备的,并不认为这是真的有必要,如果这有任何区别.
再次感谢!
我想要文本字段中的文本,例如“你的名字*”,
“你的名字”的颜色应该是黑色,* 的颜色应该是红色。
我怎样才能做到这一点??
请帮我。
出于某种原因,即使提交按钮具有属性,Chrome 似乎也会验证必填字段formnovalidate
。
<form>
Required field: <input type="text" required />
<input type="submit" formnovalidate value="do not validate" />
</form>
Run Code Online (Sandbox Code Playgroud)
单击“不验证”按钮仍会验证文本字段(在 Chrome 30.0.1599.69 m 上测试)。
有什么我做错了吗?这是一个错误吗?任何解决方法?
我已经使用HTML输入类型为我的文本框使用所需的属性.现在我的困境是,如果不填写具有必需属性的字段,我就无法离开页面.我已经尝试将causeValidation设置为false但它不起作用.我无法将我的文本框更改为asp文本框,因为它将在页面中进行大的更改.还有其他方法吗?
<asp:Button runat="server" ID="buttonCancel" CssClass="cu-btn-direction" style="float:right; margin-right: 15px; margin-bottom: 60px;" Text="Cancel" CausesValidation="false" />
Run Code Online (Sandbox Code Playgroud)
编辑:
我尝试添加验证组但没有工作...什么有效将UseSubmitBehavior设置为false http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.usesubmitbehavior%28v = vs.110%29.aspx但是如链接中所解释的那样,如果没有启用js,它将无法工作.任何其他方式?
我使用Angularjs 1.5版来验证表单中的输入.
但是,它不能使用呈现组合的自定义指令.组合根据传递给它的参数检索项目名为'listId'.然后,它使用ng-repeat在'lookupItems'上迭代.我想有些东西丢失了,比如ngModel.为什么以及如何实施它?
组合指令:
app.directive('combo', function($http) {
return {
restrict: 'AE',
template: '<div class="input-group"> <select ng-model="selectedItem">' +
'<option ng-repeat="option in lookupItems" value={{option.ListValueID}}>{{option.Translation.Value}}</option></select>' +
' {{selectedItem}} </div>',
replace: true,
scope: {
listId: '=',
defaultItem: '=',
selectedItem: '='
},
controller: function($scope) {
$http({
method: 'GET',
url: '/home/listvalues?listid=' + $scope.listId
}).then(function(response) {
$scope.lookupItems = response.data;
}, function(error) {
alert(error.data);
});
},
link: function(scope, element, attrs) {}
};
});
Run Code Online (Sandbox Code Playgroud)
html视图:迭代包含要呈现的控件类型的属性,然后根据'attribute.Required'将其设置为布尔值,这是真的.
<form name="profileAttributesForm" ng-controller="metadataCtrl" class="my-form">
<div ng-repeat="a in attributes">
<div ng-if="a.DataType == 1"> …
Run Code Online (Sandbox Code Playgroud) required-field angularjs angularjs-directive angularjs-forms
我有以下 Django 模型:
class Customer(models.Model):
email = models.EmailField(unique=True)
Run Code Online (Sandbox Code Playgroud)
在我的测试用例中,我在没有电子邮件的情况下对其进行了实例化。
class CustomerTestCase(TestCase):
def test_that_customer_can_be_created_with_minimum_data(self):
customer = Customer.objects.create()
print(customer.__dict__)
Run Code Online (Sandbox Code Playgroud)
我希望它会引发错误,但它会创建一个带有空字段的记录email
。如果我明确说null=False
和,也会发生同样的事情blank=False
。相反,它只是打印空电子邮件。
{'email': ''}
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
我已经阅读了一些相关文章,但没有成功解决我自己的让表单字段按要求显示的问题。也许问题出在外面,但我想分享一下,以防它突出一个独特的案例。
这是一个完整的课堂作业,可在此处查看,它允许用户将新列车添加到时间表并更新 Firebase 数据库。为此,我使用 Bootstrap 来设置样式,并使用一个模态弹出窗口,其中包含用于创建新列车的表单。一切正常,除了我无法将输入字段设为必填或阻止“提交”按钮。
我尝试将所有字段(在各自的 div 内)包装在一个元素中,并将它们分别包装在 a 中(如另一篇文章中所建议的),如下所示:
<div class="modal-body">
<div class="panel panel-info">
<div class="panel-body">
<div class="form-group">
<form>
<label for="train_name">Train Name:</label> <input class="form-control" id="train_name" required="" type="text">
</form>
</div>
<div class="form-group">
<form>
<label for="origin">Origin:</label> <input class="form-control" id="origin" required="" type="text">
</form>
</div>
<div class="form-group">
<form>
<label for="destination">Destination:</label> <input class="form-control" id="destination" required="" type="text">
</form>
</div>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" id="submit" type="submit" value="submit">Submit</button>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
我仔细检查了 Doctype 中的 HTML5 问题,但它检查出来了。我的结构很奇怪吗?
我有以下代码 -
视图-
<% Html.BeginForm(); %>
<div>
<%= Html.DropDownList("DropDownSelectList", new SelectList( Model.DropDownSelectList, "Value", "Text"))%>
Run Code Online (Sandbox Code Playgroud)
控制器 -
public ActionResult Admin(string apiKey, string userId)
{
ChallengesAdminViewModel vm = new ChallengesAdminViewModel();
vm.ApiKey = apiKey;
vm.UserId = userId;
vm.DropDownSelectList = new List<SelectListItem>();
vm.DropDownSelectList.Add(listItem1);
vm.DropDownSelectList.Add(listItem2);
vm.DropDownSelectList.Add(listItem3);
vm.DropDownSelectList.Add(listItem4);
vm.DropDownSelectList.Add(listItem5);
vm.DropDownSelectList.Add(listItem6);
vm.DropDownSelectList.Add(listItem7);
}
[HttpPost]
public ActionResult Admin(ChallengesAdminViewModel vm)
{
if (ModelState.IsValid)//Due to the null dropdownlist gives model state invalid
{
}
}
Run Code Online (Sandbox Code Playgroud)
ViewModel-
public class ChallengesAdminViewModel
{
[Required]
public string ApiKey { get; set; }
[Required]
public …
Run Code Online (Sandbox Code Playgroud) 假设我有一个由property_a
和组成的对象property_b
,提交时必须至少接收这两个属性之一。
如果对象只是一个,我可以使用required_without
如下验证规则
return [
'property_a' => ['required_without:property_b'],
'property_b' => ['required_without:property_a'],
];
Run Code Online (Sandbox Code Playgroud)
如果我的对象本身在另一个对象内部,那么使用点分符号会很容易:
return [
'parent_object.property_a' => ['required_without:parent_object.property_b'],
'parent_object.property_b' => ['required_without:parent_object.property_a'],
];
Run Code Online (Sandbox Code Playgroud)
但是我如何将这个验证规则与对象数组一起使用?
return [
'array.*.property_a' => ['required_without:???.property_b'],
'array.*.property_b' => ['required_without:???.property_a'],
];
Run Code Online (Sandbox Code Playgroud)
required_without的文档没有明确说明我的用例。
有解决方法吗?
我正在创建这样的表单元素:
$element = $this->CreateElement('select', 'countries');
$element->setLabel('Countries');
$element->setAttrib( 'required', true );
$element->addMultiOptions( array( ''=>'Select', '1'=>'Aus','2'=>'UK',.... ) );
Run Code Online (Sandbox Code Playgroud)
以下代码提交表单:
// form
$form = new SomeForm();
// get request
$request = $this->getRequest();
// isPost
if( $request->isPost() ) {
// isValid
if( $form->isValid( $request->getPost() ) ) {
$values = $form->getValues();
// Save values into database here
} else {
$this->view->msg = "Required Fields are missing.";
}
} else {
$this->view->msg = 'Form is not submitted properly';
}
Run Code Online (Sandbox Code Playgroud)
当我正确填写表单时,它会成功地将记录保存到数据库中.但是当我没有选择国家时,它也会提交表格.我想我应该说"缺少必填字段".消息,而不是在缺少必需字段时将值插入数据库.
我错过了什么?
谢谢
required-field ×10
forms ×3
html ×3
angularjs ×1
arrays ×1
asp.net-mvc ×1
css ×1
django ×1
field ×1
form-submit ×1
javascript ×1
jquery ×1
laravel ×1
model ×1
php ×1
textbox ×1
textfield ×1
vb.net ×1