我有这个具有不同值的下拉列表。我想做的是每当我选择其他选项时,id=othertype 的隐藏 div 显示,我将在输入字段中键入并想要保存该数据但它不起作用。我知道我不能两次使用相同的名称属性,但我不希望单独的列仅用于其他输入。是否可以将输入值保存在与选项相同的列中?我想要输入字段的值,而不是我在其他选项中传递的值,它本身就是其他选项。
<div class="form-group">
<div class="col-sm-12">
<label for="p_type">Type*:</label>
<select class="form-control" name="qp_type" id="p_type" required>
<option value="Css">CSS</option>
<option value="HTML">HTML</option>
<option value="PhP">PhP</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="form-group" id="otherType" style="display:none;">
<div class="col-sm-12">
<label for="pType">If you chose ‘Other’, please describe below:</label>
<input id="pType" type="text">
</div>
</div>
脚本
$('#p_type').on('change',function(){
if( $(this).val()==="Other"){
$("#otherType").show()
}
else{
$("#otherType").hide()
}
});
最佳答案
看起来您正在尝试使用文本字段的输入或下拉列表中的值。您可以只启用或禁用一个字段。名称可以相同,这不是问题。
<div class="form-group">
<div class="col-sm-12">
<label for="p_type">Type*:</label>
<select class="form-control" name="qp_type" id="p_type" required>
<option value="Css" selected>CSS</option>
<option value="HTML">HTML</option>
<option value="PhP">PhP</option>
<option value="Other">Other</option>
</select>
</div>
</div>
<div class="form-group" id="otherType" style="display:none;">
<div class="col-sm-12">
<label for="pType">If you chose 'Other', please describe below:</label>
<!-- START OFF WITH THIS ONE DISABLED, NAME IT THE SAME AS ABOVE --->
<input id="pType" type="text" name="qp_type" disabled>
</div>
</div>
<script>
$(document).ready(function(){
$('select[name=qp_type]').change(function(){
if($(this).val() == 'Other') {
$('#otherType').show();
$('#pType').prop('disabled',false);
}
else {
$('#otherType').hide();
$('#pType').prop('disabled',true);
}
});
});
</script>
关于javascript - 获取输入字段数据以保存在选择选项值中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39949325/