简体   繁体   English

如何禁用第二选择选项?

[英]how to disable the second select option?

<select id="title0">
   <option value="0">--- disable</option>
   <option value="1"> books</option>
</select>
<button id="save" type="submit">Save</button>

<select id="title1">
   <option value="0">--- disable</option>
   <option value="1"> books</option>
</select>
<button id="save" type="submit">Save</button>

my html select code 我的html选择代码

<script>
$(document).ready(function(){

$("#title0").change(function (){
          if($(this).val() === "0"){
            $('#save').prop('disabled', true);
          }else{
            $('#save').prop('disabled', false);
          }
        });

$("#title1").change(function (){
          if($(this).val() === "0"){
            $('#save').prop('disabled', true);
          }else{
            $('#save').prop('disabled', false);
          }
        });
});

anyone help me? 有人帮我吗? my second select(id=title1) the JavaScript doesn't work. 我的第二个select(id = title1)JavaScript无法正常工作。 it only effect for the first 1 (id=title0). 它仅对前1个(id = title0)有效。 i want to disable the button of the second select to. 我想禁用第二个选择的按钮。

Your Buttons both have the same ID ("save"). 您的按钮具有相同的ID (“保存”)。

Perhaps you can change the ID to "save1" and "save2"? 也许您可以将ID更改为“ save1”和“ save2”?

<select id="title0">
   <option value="0">--- disable</option>
   <option value="1"> books</option>
</select>
<button id="save1" type="submit">Save</button>

<select id="title1">
   <option value="0">--- disable</option>
   <option value="1"> books</option>
</select>
<button id="save2" type="submit">Save</button>

And then you can change your javascript to: 然后,您可以将JavaScript更改为:

$(document).ready(function(){

    $("#title0").change(function (){
        if($(this).val() === "0"){
            $('#save2').prop('disabled', true);
        }else{
            $('#save2').prop('disabled', false);
        }
    });

    $("#title1").change(function (){
        if($(this).val() === "0"){
            $('#save1').prop('disabled', true);
        }else{
            $('#save1').prop('disabled', false);
        }
    });
});

Edit: If you want to have the button disabled on startup, you can add the .change method at the end like this: 编辑:如果要在启动时禁用按钮,则可以在末尾添加.change方法,如下所示:

$(document).ready(function(){

    $("#title0").change(function (){
        if($(this).val() === "0"){
            $('#save2').prop('disabled', true);
        }else{
            $('#save2').prop('disabled', false);
        }
    }).change();

    $("#title1").change(function (){
        if($(this).val() === "0"){
            $('#save1').prop('disabled', true);
        }else{
            $('#save1').prop('disabled', false);
        }
    }).change();
});

This way, you define what is supposed to happen upon the "change" event and then immediately execute it. 这样,您可以定义“更改”事件后应该发生的情况,然后立即执行它。 Upon opening the page, the buttons are disabled. 打开页面后,按钮将被禁用。 You can also achieve this, if you set the disabled attribute from the beginning. 如果从头开始设置Disabled属性,则也可以实现此目的。

Your markup is invalid because there are two elements with the same ID save . 您的标记无效,因为存在两个具有相同ID save元素。

Set a different id, ie: save0 and save1 . 设置另一个ID,即: save0save1

 $(document).ready(function() { $("#title0").change(function() { if ($(this).val() === "0") { $('#save0').prop('disabled', true); } else { $('#save0').prop('disabled', false); } }); $("#title1").change(function() { if ($(this).val() === "0") { $('#save1').prop('disabled', true); } else { $('#save1').prop('disabled', false); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select id="title0"> <option value="0">--- disable</option> <option value="1"> books</option> </select> <button id="save0" type="submit">Save</button> <select id="title1"> <option value="0">--- disable</option> <option value="1"> books</option> </select> <button id="save1" type="submit">Save</button> 

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

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