【问题标题】:Adding the attribute Required to an input within Razor application with Jquery使用 Jquery 向 Razor 应用程序中的输入添加所需的属性
【发布时间】:2013-11-14 08:04:46
【问题描述】:

我有一个 asp.net mvc4 应用程序,其中我有这种形式:

  <form method="Post" action="/User/Validate_Expert_Decision" target="_parent">
      <span>
          <b style="color:red" >Votre avis</b>
          <br />
          <br />

          <input type="radio" name="avis" value="1" checked>Validé &nbsp &nbsp &nbsp &nbsp
          <input type="radio" name="avis" value="2">Refusé &nbsp &nbsp &nbsp &nbsp
          <input type="radio" name="avis" value="3">Demande de spécification &nbsp &nbsp &nbsp &nbsp
          <input type="radio" name="avis" value="4">Demande d'analyse 
          <br />
          <br />
          <br />

      </span>
    <span>
        <b style="color:red" >
        Votre justification * 
        <b />

          <br />
          <br />
      <textarea rows="16" cols="75" name="justification"></textarea>
    </span>

    <input type="hidden" name="elem" value="0"  id="elem"/>
    <p> 
      <input type="submit" name="submit">
      <input type="button" name="cancel" value="Annuler" onClick="closebox()">
    </p>
  </form>

当单选按钮采用 2、3 或 4 作为值时,我需要将属性 required 添加到 textarea justification,即仅对于第一个值,不需要 justification 输入。

那么,我该怎么做呢?

【问题讨论】:

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


    【解决方案1】:
    <script>
        $(document).ready(function () {
            $('input[name="avis"]').change(function () {
                var t = $('textarea[name="justification"]');
    
                if ($(this).val() != 1)
                    t.attr('required', 'required');
                else
                    t.removeAttr('required');
            })
        })
    </script>
    

    【讨论】:

      【解决方案2】:

      这样的吗?

      $("input[name='avis']").on("change", function () {
          var value = $(this).val();
          $('td[name=justification]').prop("required", value != "1");
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-18
        • 2013-10-10
        • 1970-01-01
        • 1970-01-01
        • 2022-08-18
        • 1970-01-01
        • 2014-09-15
        相关资源
        最近更新 更多