PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了用PHP选择值“ other”时如何显示HTML输入字段大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我想弄清楚的是,当从下拉菜单中选择“其他”的值时,如何显示html输入字段.现在,下拉列表的值来自MySQL DB查询的结果,该查询有效,但我似乎无法弄清楚如何在选择另一个选项时显示输入.

$query = MysqL_query("SELECT type FROM Dropdown_service_Type"); // Run your query

        echo '<SELEct name="service_type">'; // Open your drop down Box

        echo '<option value="NULL"></option>';
        // Loop through the query results, outpuTing the options one by one
        while ($row = MysqL_fetch_array($query)) {
         echo '<option value="'.$row['type'].'">'.$row['type'].'</option>';
        }
         echo '<option value="Other">Other</option>';
        echo '</SELEct>';// Close your drop down Box

解决方法:

使用javascript,如以下示例所示.我们可以使用style属性添加一个input字段,并认将其隐藏:
    < input name ='otherInput'id ='otherInput'type =“ text” style =“ display:none” />

var otherInput;
function checkOptions(SELEct) {
  otherInput = document.getElementById('otherInput');
  if (SELEct.options[SELEct.SELEctedIndex].value == "Other") {
    otherInput.style.display = 'block';
    
  }
  else {
    otherInput.style.display = 'none';
  }
}
<SELEct onchange="checkOptions(this)" name="service_type" id="service_type">
  <option value="NULL"></option>
  <option value="43">43</option>
  <!-- other options from your database query results displayed here -->
  <option value="Other">Other</option>
</SELEct>
<!-- the style attribute here has display none initially, so it will be hidden by default -->
<input name='otherInput' id='otherInput' type="text" style="display: none" />

有第三方库,例如jQuery,AngularJS,PrototypeJS等,可通过添加用于DOM操作的快捷方法来简化代码(尽管您应阅读this post).例如,对于jQuery,使用.on()(用于事件处理程序绑定),.show().hide()用于输入显示切换,等等:

var otherInput;
var serviCETypeInput = $('#service_type');
serviCETypeInput.on('change', function() {
  otherInput = $('#otherInput');
  if (serviCETypeInput.val() == "Other") {
    otherInput.show();
  } else {
    otherInput.hide();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<SELEct name="service_type" id="service_type">
  <option value="NULL"></option>
  <option value="43">43</option>
  <option value="Other">Other</option>
</SELEct>
<input name='otherInput' id='otherInput' type="text" style="display: none" />

大佬总结

以上是大佬教程为你收集整理的用PHP选择值“ other”时如何显示HTML输入字段全部内容,希望文章能够帮你解决用PHP选择值“ other”时如何显示HTML输入字段所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: