2016-11-16 10 views
0

.aspxページに複数選択ドロップダウンリストがあります。ASP.Netを有効/無効にするJqueryで必要なバリデータ

<asp:DropDownList ID="ddlProduct" runat="server" CssClass="form-control ui  fluid dropdown" multiple=""> 
</asp:DropDownList> 

私の要件は、それはあなたが合計数量を入力する必要があり、製品のためのテキストボックスを表示する必要があります(これは、DBから移入されている)私はドロップダウンから選択したいずれかの製品です。テキストボックスのコードは次のとおりです

 <label class="control-label">Total Quantity Purchased</label> 
      <div class="col-md-12 hideshow1" > 
      <asp:TextBox ID="Product1Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox> 
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="1Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product1Textbox"></asp:RequiredFieldValidator> 
      <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product1Textbox" 
       ValidationExpression="\d+"></asp:RegularExpressionValidator> 
      </div> 
      <div class="col-md-12 hideshow3"> 
      <asp:TextBox ID="Product2Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox> 
      <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="2Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product2Textbox"></asp:RequiredFieldValidator> 
      <asp:RegularExpressionValidator ID="RegularExpressionValidator3" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product2Textbox" 
       ValidationExpression="\d+"></asp:RegularExpressionValidator> 
      </div> 
      <div class="col-md-12 hideshow4"> 
      <asp:TextBox ID="Product3Textbox" runat="server" CssClass="form-control" MaxLength="3"></asp:TextBox> 
      <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="3Purchase quantity is required" CssClass="text-danger" ControlToValidate="Product3Textbox"></asp:RequiredFieldValidator> 
      <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ErrorMessage="Purchase quantity is invalid" ControlToValidate="Product3Textbox" 
       ValidationExpression="\d+"></asp:RegularExpressionValidator> 
      </div> 

私は、テキストボックスの表示/非表示にJqueryを使用しています。私が望むのは、RequiredFieldValidatorは選択された製品に対してのみ有効にする必要があるということです。 ":(...)未定義のプロパティ '可視' を設定することはできませんキャッチされない例外TypeError" 私は、コンソール

でこのエラーが取得

<script> 
    $('.ui.fluid.dropdown').dropdown(); 
    $('.ui.fluid.dropdown').change(function() { 
     $(".hideshow1").hide(); 
     ValidatorEnabled($("<%=RequiredFieldValidator1%>"), false); 
     $(".hideshow3").hide(); 
     ValidatorEnabled($("<%=RequiredFieldValidator3%>"), false); 
     $(".hideshow4").hide(); 
     ValidatorEnabled($("<%=RequiredFieldValidator4%>"), false); 
     $(".ui.fluid.dropdown option:selected").each(function() { 
      var selection = ""; 
      selection = ".hideshow" + $(this).val(); 
      $(selection).show(); 
      ValidatorEnabled($("<%=RequiredFieldValidator1%>"), true); 
     }); 
    }); 
    </script> 

すべてのテキストボックス区画は、デフォルトでは「表示:なし」です。

これは私のページです。 enter image description here enter image description here

答えて

0

は=、私

ValidatorEnabled($("#<%=RequiredFieldValidator1%>"), false); 
0

CustomValidatorsをお勧めします。下のバリデーターは、親の<div>の表示がblockで、TextBoxが空の場合にのみトリガーされます。私は必要なフィールドバリデータを必要と<%の前に「#」を使用して

<div style="display: none"> 
    <asp:TextBox ID="TextBox1" runat="server" ValidationGroup="myGroup"></asp:TextBox> 
</div> 

<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="TextBox1" ValidateEmptyText="true" ErrorMessage="Value is required" ValidationGroup="myGroup" ClientValidationFunction="myCustomValidation"></asp:CustomValidator> 

<script type="text/javascript"> 
    function myCustomValidation(oSrc, args) { 
     if (document.getElementById(oSrc.controltovalidate).parentElement.style.display == "block" && args.Value == "") { 
      args.IsValid = false; 
     } else { 
      args.IsValid = true; 
     } 
    } 
</script> 
+0

残念ながらそれは文句を言わない仕事のために働きました。これは、テキストボックスに何かを入力するときにだけトリガーされます –

+0

はい、あります。 'ValidateEmptyText =" true "'プロパティーを使用するので、常に起動し、ブロックが可視でテキストボックスが空のときにfalseを返します。あなたはコードを見ましたか試しましたか? – VDWWD

+0

私はコードを試しました。それは何らかの理由で[送信]ボタンでトリガされません。とにかく問題が私のコードにあったことがわかりました。 "<%= RequiredFieldValidator1%>"の代わりに "#<%= RequiredFieldValidator1%>"を使用する必要があり、チャームのように機能しました。あなたの助けをありがとう –

関連する問題