2016-07-04 12 views
0

私の連絡先フォームには、ドロップダウンリストを表示または非表示にするdivボックスがいくつかあります。私はが必要ですタグがこのdivにあります。問題は、このreqiredタグの1つが隠しdivにあるときです。フォームを送信できません。何ができるのですか?隠しdivで必要なタグを無効にする

$(document).ready(function() { 
 
    $("#Color").change(function() { 
 
    $(this).find("option:selected").each(function() { 
 
     if ($(this).attr("value") == "redd") { 
 
     $(".box").not(".redd").hide(); 
 
     $(".redd").show(); 
 
     } else if ($(this).attr("value") == "greenn") { 
 
     $(".box").not(".greenn").hide(); 
 
     $(".greenn").show(); 
 
     } else { 
 
     $(".box").hide(); 
 
     } 
 
    }); 
 
    }).change(); 
 

 
    $("#ddColor").change(function() { 
 
    $(this).find("option:selected").each(function() { 
 
     if ($(this).attr("value") == "red") { 
 
     $(".inner-box").not(".red").hide(); 
 
     $(".red").show(); 
 
     } else if ($(this).attr("value") == "green") { 
 
     $(".inner-box").not(".green").hide(); 
 
     $(".green").show(); 
 
     } else { 
 
     $(".inner-box").hide(); 
 
     } 
 
    }); 
 
    }).change(); 
 
});
.inner-box { 
 
    border: 2px solid; 
 
    height: 30px; 
 
    background-color: yellow; 
 
} 
 
.green { 
 
    background-color: green; 
 
} 
 
.red { 
 
    background-color: red; 
 
}
<div> 
 
    <fieldset> 
 
    <p dir="rtl"> 
 
     <label>case1</label> 
 
     <select id="Color" required="required"> 
 
     <option>please select</option> 
 
     <option value="redd">home 
 
      <option> 
 
      <option value="greenn">laptop</option> 
 
     </select> 
 
    </p> 
 
    </fieldset> 
 
</div> 
 
<div class="redd box"> 
 
    <div> 
 
    <fieldset> 
 
     <p dir="rtl"> 
 
     <label>case2</label> 
 
     <select id="ddColor" required="required"> 
 
      <option>please select</option> 
 
      <option value="red">sell 
 
      <option> 
 
       <option value="green">rent</option> 
 
     </select> 
 
     </p> 
 
    </fieldset> 
 
    </div> 
 
    <div class="red inner-box"> 
 
    <input dir="rtl" type="text" name="pricerange" required /> 
 
    </div> 
 
    <div class="green inner-box"> 
 
    <input dir="rtl" type="text" name="rentrange" required/> 
 
    </div> 
 

 
</div> 
 
<div class="greenn box"> 
 
    <input dir="rtl" type="text" name="ramrange" required/> 
 
</div>

+0

1.入力が「非表示」であることを示すコードはありません。それはCSSによって隠されていることを意味しますか?なぜそれが隠されているのですか?あなたはそれをプログラムで見ることができますか?あなたの下にあるすべてのコードは制御されていますか?あなたの質問を編集してください。 – morels

+0

お返事ありがとうございました。あなたがドロップダウンリストの選択とJavaScriptのコードをCSSで表示せずに入力divを表示または非表示にした場合 – Malekian

答えて

0

をあなたが隠したときにそれらにfalseからrequiredを設定する必要があります、と示すとき、再びtrueします。それを示すとき、それらのdiv内inputsの全てが必要な場合は簡単です。

例としてあなた.reddのdivを使用して:

隠す:

$(".redd").hide().find("input").prop("required", false); 

表示:

$(".redd").show().find("input").prop("required", true); 

のうちが必要な場合は、その後、彼らの古い設定を覚えているし、したいでしょう、それを再適用:

隠す:

$(".redd").hide().find("input").each(function() { 
    var $input = $(this); 
    $input.data("was-required", $input.prop("required")); 
    $input("required", false); 
}); 

表示:

$(".redd").show().find("input").each(function() { 
    var $input = $(this); 
    $input.prop("required", $input.data("was-required")); 
}); 

あなたがマークアップに隠されたいくつかのから始める場合、あなたはよその上showアクションのいずれかの前にこれをしたい:

$("input").each(function() { 
    $(this).data("was-required", this.required); 
}); 

... initializへeフラグ。彼らがすべて表示を開始した場合、上記の「非表示」アクションが行われるため、必要はありません。

+0

私はちょっと私が私のJavaScriptコードでコードを追加する必要がある混乱しているあなたの応答のために非常にありがとう?これを見せてくれますか? – Malekian

+0

@ user6362236: '$("。redd ")。hide()'と '$("。redd ")。show()'と類似しています。それが複数の場所にある場合は、おそらくそれを名前付き関数に入れて呼び出します。 –

+0

入力の代わりにdivボックスにドロップダウン選択があるかどうか質問があります。このコード.find( "input")。私は何ができますか? – Malekian

0

あなたが使用することができattrに削除するには:

$('#id').removeAttr('required');​​​​​ 
+0

お返事ありがとうございます。私はdivに基づいて必要なタグを削除したい。私はこのコードはこれを行うことはできないと思う。 – Malekian

+0

はい...その場合、@ T.Jに従うことができます。 Crowderの答え...それは動作します:) –

関連する問題