最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to enabledisable drop down list box in HTML + JavascriptJQuery? - Stack Overflow

programmeradmin0浏览0评论

I have 2 drop down lists of pairs of name IMEI's and cities, as following:

<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>

The desired behavior is: when one selects a city name, the IMEI list enables; otherwise it is disabled.

How can I achieve this?

Thanks in advance!

I have 2 drop down lists of pairs of name IMEI's and cities, as following:

<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>

The desired behavior is: when one selects a city name, the IMEI list enables; otherwise it is disabled.

How can I achieve this?

Thanks in advance!

Share Improve this question edited Mar 4, 2014 at 9:21 dnl-blkv 2,1281 gold badge17 silver badges23 bronze badges asked Mar 4, 2014 at 9:13 user3377703user3377703 2
  • 1 possible duplicate of stackoverflow./questions/7703241/… – amudhan3093 Commented Mar 4, 2014 at 9:16
  • 1 Just to point out the obvious - your city dropdown will already have "Jhansi" selected. As a user I would be mighty annoyed to have to select a different city in order to enable the IMEI dropdown, if I wanted to select Jhansi. – Reinstate Monica Cellio Commented Mar 4, 2014 at 9:20
Add a ment  | 

6 Answers 6

Reset to default 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()

Try this,

<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>

Script

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

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

Demo:http://jsfiddle/khmSm/

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';
});

try this way

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);
   });

LIVE DEMO:

http://jsfiddle/dreamweiver/TvpRF/7/

Happy Coding :)

<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>   

Here is the link : http://jsfiddle/khmSm/1/

Disable select and update the plugin. Use trigger chosen:updated to disable the select widget.

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

评论列表(0)

  1. 暂无评论