【问题标题】:remove required property from input field on form submit从表单提交的输入字段中删除所需的属性
【发布时间】:2013-08-09 07:59:34
【问题描述】:

型号:

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

表格:

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

脚本:

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

如果满足某些条件,我想删除所需的属性。无法以这种方式执行此操作。我该如何实现?

【问题讨论】:

  • 使用 Jquery: $("#city").removeAttr("required");或使用 document.getElementById("city").removeAttribute('required');
  • 你在使用 JqueyVal 吗?如果是,删除所需的不是唯一的事情!

标签: javascript jquery html asp.net-mvc-4


【解决方案1】:

JavaScript 区分大小写。

使用

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

Demonstration

请注意,当您尝试在元素存在之前访问该元素时,您的代码无法正常工作。如果您不在事件上执行代码,请将您的脚本放在元素之后:

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

还请注意,您不能在提交函数中更改它并期望您的表单被提交,因为为时已晚:如果未填写必填字段,则不会调用此事件处理程序!

【讨论】:

  • 表单仍然没有提交,我正在表单提交功能中这样做
  • @user2137186​​ 我已经编辑了我的答案以指出另一个问题。它可能相关或不相关,具体取决于您显示的代码与您实际拥有的代码相差多远。
  • 我在表单提交函数上调用那个脚本函数
  • 但是你的提交函数被调用了吗?如果 required 为 true 并且该字段为空,则不应调用它。您必须在其他地方更改它,例如通过绑定到按钮单击事件。
  • @dystroy 鉴于他正在使用 MVC4,可能存在一个 JqueryVal。所以删除所需的还不够。
【解决方案2】:

你可以使用:

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

或使用 jQuery

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

【讨论】:

  • 表单仍然没有提交,我正在表单提交功能中这样做
  • 我正在使用.net mvc4 ...所以这不应该在这里发布
  • @user2137186​​ 你可以发帖,只是!我们也会为 mvc 重新标记它!
【解决方案3】:

你应该这样做:

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

【讨论】:

  • 表单仍然没有提交,我正在表单提交功能中这样做
  • 你应该发布整个函数。
  • 你这是什么意思?
  • 你应该把整个表格和功能贴出来,还有其他问题!
【解决方案4】:

On Submit 函数要删除所需的属性时,表单已经通过了验证。这些步骤是首先您必须禁用验证,然后您才能提交必填字段为空的表单。接下来删除所需的属性,最后通过$.validator.unubtrusive.parse(form) 手动调用验证,这将通过调用其他验证类型(例如string lengthformatting)来验证表单,除了所需的属性之外的所有其他内容。检查if form.valid() then submit() else什么都不做。

【讨论】:

    【解决方案5】:

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

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

    【讨论】:

      【解决方案6】:

      不确定是否有人找到了更好的方法来做到这一点,它可能不是最有效的方法,但你可以使用 PHP 来完成。如果在页面加载之前知道您要满足的条件,那么您可以这样做:

      <input type="text" id="city" 
      
      <?php
           if (condition_is_met) {
                echo ">";
           } else {
                echo "required>";
           }
      ?>
      

      【讨论】:

        【解决方案7】:

        如果您将文本字段放在表单中,则可以使用称为 novalidate 的特殊属性覆盖必填字段。这是语法:&lt;form novalidate&gt; .. &lt;/form&gt;

        这是一个例子:

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

        The novalidate Attribute 这是两个使用 novalidate 属性的现场演示的链接。

        【讨论】:

          【解决方案8】:

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

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-03
            • 1970-01-01
            • 2011-12-12
            • 1970-01-01
            相关资源
            最近更新 更多