简体   繁体   English

如何禁用下拉列表

[英]How to disable drop down list

I have this drop down list and my idea is when i click on videos that i disable other drop down list.This is not working.Do i need something else? 我有这个下拉列表,我的想法是当我单击禁用其他下拉列表的视频时,这不起作用。我还需要其他东西吗?

<select name="Type" id="type">
  <option value="0">Please Select</option>
  <option name="image" value="I" >Images</option>
  <option name="video "value="V">Videos</option>
  <?php 
$value=$_POST['image'];
?>
</select>

<select id="text-one" <?php if($value=="V"){ ?> disabled="disabled"<?php }?> >
                <option selected value="base">Please Select</option>
                <option value="Dig">Digital</option>
    </select

You can do this with PHP, but that's going to require submitting form the data so you can then access $_POST , and then loading a new page, that's basically the same as the original, just with text-one disabled. 您可以使用PHP进行此操作,但是这需要提交表单数据,以便您可以访问$_POST ,然后加载一个新页面,该页面与原始页面基本相同,只是禁用了文本一键。

Remember, PHP runs server side, and once it's rendered in the browser, nothing else can be done via PHP. 记住,PHP在服务器端运行,并且一旦在浏览器中呈现,就无法通过PHP进行其他操作。 For example, you've got a line in your sample code that show's you tring to assign $_POST['image'] to $value - until you submit a form, $_POST will be empty. 例如,你有一条线在你的示例代码,告诉你特林分配的$_POST['image']$value -直到你提交一个表单, $_POST将是空的。

Most likely, you want to do this client side and without a reload, and this can be done using javascript. 最有可能的是,您希望在不重新加载的情况下进行此客户端操作,而这可以使用javascript完成。

As a basic overview: 作为基本概述:

  1. monitor the onChange event handler for your type select element 监视您的type选择元素的onChange事件处理程序
  2. check the value of the type select element, and set the disabled attribute on the text-one as needed 检查type选择元素的值,并根据需要在text-one上设置disabled属性

Another option (maybe simpler?): 另一个选择(也许更简单?):

attach an onclick attribute to the video input, that will run a javascript function that disables text-one onclick属性附加到视频输入,该属性将运行禁用text-one一的javascript函数

jQuery will make some of this easier, but you could write all of the above in plain javascript, without any libraries. jQuery将使这方面的工作变得更容易,但是您可以使用简单的javascript编写以上所有内容,而无需任何库。

try this code : 试试这个代码:

<script>
  function disable(val)
  {
      if(val=="V")
           document.getElementById("text-one").disabled=true;
      else
          document.getElementById("text-one").disabled=false;
  }
</script>

<select name="Type" id="type" onchange="disable(this.value)">
  <option value="0">Please Select</option>
  <option name="image" value="I" >Images</option>
  <option name="video "value="V">Videos</option>
</select>

<select id="text-one" >
      <option selected value="base">Please Select</option>
       <option value="Dig">Digital</option>
</select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM