【问题标题】:What is the best way to enable the next Input box when another Input box is changed更改另一个输入框时启用下一个输入框的最佳方法是什么
【发布时间】:2020-10-29 15:40:19
【问题描述】:

我有类似的东西:

<input id="1" type="text"></input>
<input id="2" type="text" disabled></input>

当我更改 id="1" 的内容时,我想启用 id="2"。我可以让它以这种格式工作,但是,我需要它在一个特定的函数中而不是一个通用的输入 onchange 事件中,因为这个页面有很多输入:

$(document).ready(function(){
 $("input").change(function(){
  $(this).next().prop("disabled", false); // Element(s) are now enabled.
 });
});

我相信我的问题是传递“这个”信息以便能够更改下一个元素。我已经尝试了下面的代码行,但无法让它们工作。忽略我放在那里的警报,我只是想看看传递了什么信息。另外,我也传递了一个带有 onchange 事件的字符串。

$(el.id).next().prop("disabled", false);

function myFunction (str, el) {
 var newId = el.id;
 alert(newId);
 $(newId).next().prop("disabled", false);
}

function myFunction (str, id) {
 $(id).next().prop("disabled", false);
}

更新代码:

  function Update(str, el){
  var xmlhttp=new XMLHttpRequest();
  xmlhttp.onreadystatechange=function()
     {
      if (xmlhttp.readyState==4) {
        if (xmlhttp.status==200) {
                    var newId = el.id;
                    alert(newId);
                    $(newId).next().prop("disabled", false);
        }
        else {

        }
      }
     };
    var Info = str;
    var queryString = Info;
    xmlhttp.open("POST","Reduced.php",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send(queryString);
    }

【问题讨论】:

    标签: javascript jquery next attr prop


    【解决方案1】:

    改用keyup 事件监听器。

    $(document).ready(function(){
     $("input[type=text]").keyup(function(){
      $(this).next().prop("disabled", false); // Element(s) are now enabled.
     });
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <input type="text">
    <input type="text" disabled>
    <input type="text" disabled>
    <input type="text" disabled>
    <input type="text" disabled>

    【讨论】:

    • 输入类型为文本。将其更改为 type=text 不起作用。
    • 是的,我原来的答案是复选框。我只是使用一个复选框进行测试。我的答案已适当修改。
    • 它需要保持为“change”而不是“keyup”。我可以让它工作,但它仍然是一种解决方法。我需要在函数中使用它,因为我正在通过 onchange 事件传递一个字符串。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多