简体   繁体   English

从表单提交的输入字段中删除必需的属性

[英]remove required property from input field on form submit

Model: 模型:

[Display(Name = "City"]
[Required]
[RegularExpression(@"^(?!\d$).*$"]
[StringLength(20,MinimumLength = 2]
public string City { get; set; }

Form: 形成:

@Html.LabelFor(x => x.City, new { @class = "control-label" })
@Html.TextBoxFor(x => x.City, new {id="city" })

Script: 脚本:

<script>
  $(document).ready(function () {   
    $("#identificationForm").submit(function (e) {
      var required=document.getElementById("city").required;
      console.log(required);
      // e.preventDefault();
     });
  });
</script>

I want to remove required property if some condition is met.Unable to do this this way.How can i achieve this? 如果满足某些条件,我想删除所需的属性。无法以这种方式执行此操作。如何实现此目的?

JavaScript is case sensitive. JavaScript区分大小写。

Use 使用

document.getElementById("city").required = false;

Demonstration 示范

Be careful that your code can't work as you try to access the element before it exists. 当您尝试在元素存在之前访问该元素时,请注意您的代码无法正常工作。 Put your script after the element if you don't execute the code on an event : 如果不在事件上执行代码,请将脚本放在元素后面:

<input type="text" id="city" required>
<script>
if(somecondition is true){
    document.getElementById("city").required = false;
}
</script>

Note also that you can't change this in a submit function and expect your form to be submitted because it's too late : this event handler won't be called if the required field is not filled ! 另请注意,您无法在提交函数中更改此内容并期望提交表单,因为为时已晚:如果未填写必填字段,则不会调用此事件处理程序!

You can use: 您可以使用:

document.getElementById("city").removeAttribute("required");

or with jQuery 或者使用jQuery

$('#city').removeAttr('required')

You shoud do this: 你应该这样做:

if(somecondition is true)
{
  var city = document.getElementById("city");
  city.removeAttribute('required');
}

By the time the On Submit function is to remove the required attribute, the form would have already passed validation. On Submit函数删除所需属性时,表单已经通过验证。 The steps are first you have to disable validation then you will be able to submit the form with the required fields empty. 首先,您必须禁用验证,然后您将能够提交必填字段为空的表单。 Next remove the required attributes and finally manually invoke validation via $.validator.unubtrusive.parse(form) and this will now validate the form by invoking the other validation types such as string length and formatting , everything else except the required attributes. 接下来删除所需的属性,最后通过$.validator.unubtrusive.parse(form)手动调用验证,现在将通过调用其他验证类型(如string lengthformatting以及除必需属性之外的所有其他内容来验证表单。 Check if form.valid() then submit() else do nothing. 检查if form.valid() then submit() else什么都不做。

在php中非常简单,这不会在提交时验证您的表单,

<input type="submit" onclick="return confirm('Are you sure?')" value="Abort Test" formnovalidate>

Not sure if anyone has figured out a better way to do this yet and it might not be the most efficient way to do it but you could use PHP to get this done. 不确定是否有人已经找到了更好的方法来做到这一点,它可能不是最有效的方法,但你可以使用PHP来完成这项工作。 If the condition you are looking to meet is known before the page loads then you can have this: 如果在页面加载之前已知您要满足的条件,那么您可以这样:

<input type="text" id="city" 

<?php
     if (condition_is_met) {
          echo ">";
     } else {
          echo "required>";
     }
?>

If you put your text fields in a form, it is possible to overwrite the required field with a special attribute called novalidate . 如果将文本字段放在表单中,则可以使用名为novalidate的特殊属性覆盖必填字段。 This is the syntax: <form novalidate> .. </form> 这是语法: <form novalidate> .. </form>

Here is an example: 这是一个例子:

<form novalidate>
  Zip code: <input type="text" name="zipcode" pattern="[0-9]{5}" required><br/>

  <input type="submit">
</form>

The novalidate Attribute Here is a link with two live demos using novalidate attribute. novalidate属性以下是使用novalidate属性的两个实时演示的链接。

或者在提交表单的那一刻,如果它为null,则可以禁用它:

if($("#City").val().trim().length == 0) $("#City").attr("disabled", true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM