如何在HTML + Javascript / JQuery中启用/禁用下拉列表框?

时间:2014-03-04 09:13:24

标签: javascript jquery html

我有两个名称为IMEI和城市的下拉列表,如下所示:

<HTML>

 <body>
Select Programming font:
<select name="IMEI">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">$</option>
</select>

<select name ="city">
  <option value="jhansi">Jhansi</option>
  <option value="Delhi">Delhi</option>
  <option value="Mumbai">Mumbai</option>
  <option value="Kanpur">Kanpur</option>
</select>
</body>
</html>

期望的行为是:当选择城市名称时,IMEI列表启用;否则它被禁用。

我怎样才能做到这一点?

提前致谢!

6 个答案:

答案 0 :(得分:2)

Fiddle Demo

$(function () {
    var sel_imie = $('select[name="IMEI"]');
    sel_imei.prop('disabled', true); //disable IMEI select
    $('select[name ="city"]').change(function () {
        sel_imei.prop('disabled', false); //enable when value of city select is changed
    });
});

.change()

答案 1 :(得分:1)

试试这个,

<select name="IMEI" disabled="disabled" id="IMEI">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">$</option>
</select>

<select name ="city" id="city">
  <option value="jhansi">Jhansi</option>
  <option value="Delhi">Delhi</option>
  <option value="Mumbai">Mumbai</option>
  <option value="Kanpur">Kanpur</option>
</select>

<强>脚本

$('#city').on('change',function(){

$('#IMEI').attr('disabled',false);
});

演示:http://jsfiddle.net/khmSm/

答案 2 :(得分:0)

Select Programming font:
<select name="IMEI" disabled="disabled">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
    <option value="4">$</option>
</select>
<select name="city">
    <option value="">Select...</option>
    <option value="jhansi">Jhansi</option>
    <option value="Delhi">Delhi</option>
    <option value="Mumbai">Mumbai</option>
    <option value="Kanpur">Kanpur</option>
</select>

document.getElementsByName('city')[0].addEventListener('change', function(e){
    var imei = document.getElementsByName('IMEI')[0];
    var city = e.target;
    imei.disabled = city.value != '' ? '' : 'disabled';
});

答案 3 :(得分:0)

试试这种方式

HTML CODE:

   CITY :<select name="city">
           <option value="jhansi">Jhansi</option>
           <option value="Delhi">Delhi</option>
           <option value="Mumbai">Mumbai</option>
           <option value="Kanpur">Kanpur</option>
         </select>
         <br/>

   IMEI :<select name="IMEI">
           <option value="1">1</option>
           <option value="2">2</option>
           <option value="3">3</option>
           <option value="4">$</option>
         </select>

JQUERY CODE:

   $('select[name=IMEI]').prop('disabled',true);
   $('select[name=city]').on('change',function () {
        $('select[name=IMEI]').prop('disabled',false);
   });

现场演示:

http://jsfiddle.net/dreamweiver/TvpRF/7/

快乐编码:)

答案 4 :(得分:0)

<select name="IMEI" disabled="disabled" id="IMEI">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>

<select name ="city" id="city">
 <option value ="choose">Choose a city</option>
 <option value="jhansi">Jhansi</option>
 <option value="Delhi">Delhi</option>
 <option value="Mumbai">Mumbai</option>
 <option value="Kanpur">Kanpur</option>
</select>   

以下是链接:http://jsfiddle.net/khmSm/1/

答案 5 :(得分:0)

禁用选择并更新插件。使用已选择的触发器:已更新以禁用选择小部件。

$('#RefundType').prop('disabled', true).trigger("chosen:updated");