我正在寻找解决方案,但没有找到一个好的例子。
我有表单下拉菜单和输入字段。现在我希望根据下拉列表中的选择需要输入字段。我想要"要求"动态基于选择。
这是我的表格:
<form name="coaching_save" method="POST" action="coaching_form.php" class="form-horizontal form-label-left">
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="Issue">Issue <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<select name="Issue" id="Issue" required class="form-control">
<option disabled selected hidden></option>
<?php
$tww_dd_sql = "SELECT * FROM `coaching_issue`";
$tww_dd_result = $mysqli->query($tww_dd_sql);
while($row = $tww_dd_result->fetch_array())
{
$row['id'];
echo "<option value='".$row['issue']."'>".$row['issue']."</option>";
}
?>
</select>
</div>
</div>
<div class="item form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="Qfinity_ID_1">Qfinity ID <span class="required">*</span>
</label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" required id="Qfinity_ID_1" name="Qfinity_ID_1" class="form-control col-md-7 col-xs-12">
</div>
</div>
<div class="col-md-6 col-md-offset-3">
<button id="send" type="submit" class="btn btn-success">Save</button>
</div>
</form>
所以在输入字段&#34; Qfinity_ID_1&#34;我想添加&#34; required&#34;什么时候&#34; SbS&#34;从下拉查询中选择(还有许多其他值可供选择)
答案 0 :(得分:2)
您需要在此处使用一些JavaScript。如果你正在使用jQuery,那就可以了:
$(document).on('change','#Issue',function(){
if($(this).val() === 'SbS') {
$("#Qfinity_ID_1").prop('required',true);
} else {
$("#Qfinity_ID_1").prop('required',false);
}
});