简体   繁体   English

如何根据用户所做的选择将只读下拉列表/文本框更改为读/写?

[英]How can I change read only dropdowns/text boxes to read/write depending on selection made by user?

It will have one text box (txtGRC) which will be read/write no matter what. 它将具有一个文本框(txtGRC),无论如何,该文本框都将被读取/写入。

However, I would like to have the rest of the boxes greyed out until an input has been made into txtGRC. 但是,我想将其余的框都显示为灰色,直到已向txtGRC中输入内容为止。

My problem is a bit more complicated than this but once this has been solved I believe that I can take over the rest. 我的问题要比这复杂得多,但是一旦解决,我相信我可以接管其余的工作。 It's funny, I could do this easily on VB. 很好笑,我可以在VB上轻松完成此操作。

Here is the code for the container: 这是容器的代码:

<content id="GenerateReportContent" class="col-md-4">
    <form id="GenerateReportContainer">
        <div class="AltFunctions">
            <ul>
                <li>
                    <a href="#" class="AltButton" id="altClearButton">Clear</a>
                </li>
                <li>
                    <a href="#" class="AltButton" id="GRaltInfoButton">Info</a>
                </li>
            </ul>
        </div>
        <h1 id="GenerateReportHeader">GENERATE REPORT</h1>
        <input type="text" id="txtGRC" class="form-control" placeholder="Enter Specific Claim Number...." >
        <p id="txtOptional">(optional)</p>
        <select id="GRDropDown">
            <option value="SelectReport">Select Report</option>
        </select>
        <br />

        <select id="GRDropDown">
            <option value="SelectSupplierName" disabled="disabled">Select Supplier Name</option>
        </select>
        <br />
        <select id="GRDropDown">
            <option value="SelectReviewPeriod" disabled="disabled">Select Review Period</option>
        </select>
        <div class="form-group">
            <input type="text" id="txtGRC" class="form-control" placeholder="Month Code..." disabled="disabled">
            <p id="txtOptional">(optional)</p>

        </div>
        <button type="submit" id="GenerateReportButton" class="btn btn-default">GO</button>
    </form>
</content>

If you need more information just place a comment and I will do what I can. 如果您需要更多信息,请发表评论,我将尽我所能。 Thank you in advance. 先感谢您。

With javascript / jQuery, you will have to handle a keypress on that textbox, and if the length of the value of the textbox is bigger than 0, or the length you want, then, foreach other input disabled, you will enable it. 使用javascript / jQuery,您将必须处理该文本框上的按键,并且如果文本框的值的长度大于0或所需的长度,则禁用每个其他输入,则将其启用。

An example: 一个例子:

$(".txtGRC").keyup(function() {
    if( $(this).length > 0 ) {
        $("input.disabled").each(function() {
            $(this).prop("disabled", false);
        });
    }
    else {
        $("input.disabled").each(function() {
            $(this).prop("disabled", true);
        });
    }
});

So basically the code above (with jQuery) checks whenever the user release the key pressed and calculates the length of the textarea, if this is bigger than 0, it enables de inputs with the class ".disabled" (here you should put the inputs that starts disabled), otherwise, it disables them. 因此,基本上以上代码(使用jQuery)都在用户释放按键时进行检查,并计算文本区域的长度,如果该长度大于0,则会启用“ .disabled”类的de输入(在此处应将输入放以禁用开始),否则,将其禁用。

First, the "disabled" attribute should be applied to the "select" tag not "option". 首先,应将“已禁用”属性应用于“选择”标记而非“选项”。

<select id="GRDropDown" disabled>

Now, for your "txtGRC" field, you have three attributes to choose from (as event handlers): 现在,对于“ txtGRC”字段,您可以选择三个属性(作为事件处理程序):

  • oninput 输入
  • onpaste 糊剂
  • onkeydown 按键

depending on the browser the client will be using. 取决于客户端将使用的浏览器。

<input type="text" id="txtGRC" class="form-control" placeholder="Enter Specific Claim Number...." onkeydown="valueChanged()">

Finally, add the javascript function to disable/enable "GRDropDown" dropdowns: 最后,添加javascript函数以禁用/启用“ GRDropDown”下拉列表:

<script>
    function valueChanged() {
        var val = document.getElementById("txtGRC").value;
        var dd = document.getElementById("GRDropDown");
        if(val.length>0) dd.disabled=false;
        else dd.disabled = true;
    }
</script>

If you have jQuery previously included in your code, the solution would be simpler. 如果您的代码中以前包含了jQuery,则解决方案会更简单。

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

相关问题 可以在代码镜像中将特定文本设为只读吗? - Can specific text be made read-only in Code Mirror? 如何根据用户选择动态更改多个复选框的编号? - How to change the numbering of multiple check-boxes dynamically depending upon user selection? 如何在 Cordova 中读取/写入本地 json 或文本? - How can I read / write a local json or text in Cordova? 如何在JavaScript中从纯文本读取/写入 - How can I read/write from a plain text in JavaScript 根据用户从-选择下拉菜单中所做的选择-如何启用/禁用某些链接并更改其颜色 - How to enable/disable and change color of some links depending on the selection user have made from a - slect dropdown menu - 如何更改整个分区中的文本框大小? - How can I change the text boxes size in an entire division? 我怎样才能使淘汰赛“价值”绑定只写,这样它就不会尝试从中读取? - How can I make a knockout "value" binding write-only so it doesn't try to read from it? 如何忽略上一个选择,只写上一个选择 - How can I ignore previous selection and write only last selected 允许将dynamodb只读用户的密钥公开是错误的吗? - is it wrong to allow a secret key for dynamodb read only user to be made public? 根据下拉菜单中的选择更改文本 - Change text depending on selection in dropdown
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM