【问题标题】:disable the input if selected from an item如果从项目中选择,则禁用输入
【发布时间】:2011-09-15 23:30:27
【问题描述】:

我的源代码中有以下内容,我希望能够将 company 和 dock 设为灰色,如果有人选择运输或存储则禁用

<select name="myselect" id="myselect">

<option value="shipping">Shipping</option>
<option value="Recieving" selected="selected">Reciving</option>
<option value="Storage">Storage</option>
</select>

company:<input name="company" type="text"  id="company" /></div>
dock:<input name="dockingStation" type="text"  id="dock" />

编辑

我已经尝试过了,但现在它删除了禁用的属性,但没有添加回来

  <select name="myselect" id="myselect">

        <option value="shipping">Shipping</option>
        <option value="Recieving" selected="selected">Reciving</option>
        <option value="Storage">Storage</option>
        </select>

        company:<input name="company" type="text"  id="company" **disabled="disabled**"/>
        dock:<input name="dockingStation" type="text" id="dock" disabled="disabled" />

jquery 是

$("#myselect").change(function() {
    var val = $(this).val();
    if(val == "shipping" || val == "storage") {
         $("#company").attr("disabled", true)  // this works

} 其他 { $("#company").removeAttr("disabled") 。重点() } });

但现在一旦启用我就无法再次禁用它

编辑

现在可以使用

【问题讨论】:

    标签: cakephp jquery cakephp-1.3


    【解决方案1】:
    $("#myselect").change(function() {
        var val = $(this).val();
        if(val == "shipping" || val == "storage") {
            $("#company, #dock").prop("disabled", true)
        } else {
            $("#company, #dock").prop("disabled", false)
        }
    });
    

    或者如果您更喜欢更短的解决方案:

    $("#myselect").change(function() {
        var val = $(this).val();
        $("#company, #dock").prop("disabled", (val == "shipping" || val == "storage"));
    }).change(); // optional, set the correct state if you don't know what
                 // the initial selected option will be on page load
    

    简要说明:

    • 将 onchange 处理程序绑定到选择元素。
    • 如果所选选项的值为“shipping”或“storage”,则禁用这两个输入,否则,启用它们。

    见:

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

    【讨论】:

    • 它没有在输入中添加 disabled="disabled"
    • @Autolycus - 你到底是什么意思?
    • 它没有禁用输入
    • 您可能使用的是 1.6 之前的 jQuery 版本。我应该提到.prop 是一种新方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 2021-03-09
    • 2019-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多