ユーザーがドロップダウンから「はい」を選択すると、2つの追加フィールドが表示されるフォームを作成しようとしています。これらのフィールドは両方とも必要であり、そのうちの1つは 'コード'の配列に従って検証する必要があります。ユーザーは、フォームが正しく送信されるためには配列のコードの1つを入力する必要があります。ただし、ユーザーがドロップダウンから「いいえ」を選択した場合、これらのフィールドは表示されず、必須でもありません。ではなくが発生し、フォームを送信できます。JavaScript HTML - ユーザーに表示される余分な必須フォームフィールド
私はこのためにいくつかのコードを持っていますが、フィールドを表示することはできません。私がこれで遭遇した以前の問題(配列検証を除いて - コードを破って余分なフィールドが表示されなくなったことを含む)は、ユーザーが「はい」を選択してから心を変えて「いいえ」を選択した場合フォームは提出されず、入力されるフィールドが正確に入力されている必要があります/入力された正しい配列値。
誰でもこの作品を作るのを手伝ってもらえると大変感謝しています。
HTML:
<form id="form" method="post" action="action.php">
<div class="form-group">
<label class="control-label">Defect?</label>
<select onclick='checkIfYes()' class="select form-control" id="defect" name="defect">
<option id="No" value="No">No</option>
<option id="Yes" value="Yes">Yes</option>
</select>
</div>
<div id="extra" name="extra" style="display: none">
<label class="control-label" for="desc">Description</label>
<input class="form-control" type="text" id="desc" name="desc" required disabled>
<label class="control-label" for="auth_by">Authorised By</label>
<input class="form-control" type="text" id="auth_code_input" name="auth_by" required disabled>
</div>
<hr>
<div class="form-group">
<button class="btn btn-info" id="submit" name="submit" type="submit">Submit
</button>
</div>
</form>
JavaScriptを:それはあなたが関数を定義する方法でグリッチが
$(document).ready(function() {
checkIfYes = function checkIfYes() {
if (document.getElementById('defect').value == 'Yes') {
// show extra fields & make them required
document.getElementById('extra').style.display = '';
document.getElementById('auth_code_input').disabled = false;
document.getElementById('desc').disabled = false;
// show user if their input is one of the codes in the array when leaving field
$('#auth_code_input').blur(function() {
if (!ValidateInput()) {
e.preventDefault();
}
});
// prevent form submission if input is not one of the codes in the array
$('#auth_form').on('submit', function(e) {
if (!ValidateInput()) {
e.preventDefault();
}
});
function ValidateInput() {
var codes = ['Foo', 'Bar']; // user must enter one of these
var IsValid = false;
var input = $('#auth_code_input').val()
if (codes.indexOf(input) > -1) { // if input equals one of the codes in the array
$('#iconBad').removeClass('glyphicon-remove').addClass('glyphicon-ok');
IsValid = true;
} else {
$('#iconBad').removeClass('glyphicon-ok').addClass('glyphicon-remove');
IsValid = false;
}
return IsValid;
}
} else { // hide and disable extra fields so form can submit
document.getElementById('extra').style.display = 'none';
document.getElementById('auth_code_input').disabled = true;
document.getElementById('desc').disabled = true;
}
}
});
フィドルでは動作しません。 checkIfYes()関数をcheckIfYes = function checkIfYes()に変更する必要があります。ここでフィドルが更新されています:http://jsfiddle.net/snowMonkey/z6Lm1m5x/24/ – Snowmonkey
私はまだ問題が発生しています。 'yes'に変更されますが、 'no'に変更されると、フォームの送信が許可されません。 – sinesine
ええ、妥当性検査は現在checkIfYesの内部で定義されているため、妥協しています - イベントリスナーをjavascriptのルートレベルに移動します。パフォーマンスに影響しません。下の私の答えを見て、うまく動作します。 – Snowmonkey