【问题标题】:Not able to display data on change of drop down values无法显示有关下拉值更改的数据
【发布时间】:2018-05-15 05:32:05
【问题描述】:

我有一个动态填充数据的表。每行在状态部分下都有一个下拉菜单,用户可以从中选择一个值,根据选择的值,必须显示一些输入框,但这些框应该显示在下拉列表所属的行中。因此,如果下拉列表属于第一行,则该框应仅出现在第一行,如果下拉列表属于第二行,则该框应仅出现在第二行中

目前,我使用的代码是在不同的行中随机显示输入框,而与下拉位置无关

谁能告诉我如何纠正这个问题 HTML 代码

<table class="table table-bordred table-striped">
  <thead>
    <th>NAME</th>
    <th>STATUS</th>
  </thead>
  <tbody>
    <?php foreach($student as $per_student): ?>
      <tr>
        <td><?php echo $per_student->name; ?></td>
        <td>
          <select class="can-dd" onchange="myFunction(this)" name="status">
            <option value="">STATUS</option>
            <option value="Accepted ">Accepted</option>
            <option value="Forwarded ">Forwarded </option>
            <option value="Schedule">Schedule </option>
            <option value="Custom">Custom</option> 
          </select>
          <div class="schedule">
            <input  type="text" placeholder="schedule" name="schedule">
            <button type="submit" class="btn btn-primary "  >SUBMIT</button>
          </div>

          <div class="custom">
            <input  type="text" placeholder="custom"  name="custom">
            <button type="submit" class="btn btn-primary "  >SUBMIT</button>
          </div>

          <div class="submit_value">
            <button type="submit" class="btn btn-primary "  >SUBMIT</button>
          </div>
        </td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

脚本

<script type="text/javascript">
  function myFunction(selectObject) {
    var value = selectObject.value;
    if(value == 'Schedule'){
      jQuery('.custom').remove();
      jQuery('.submit_value').remove();
      $(".schedule").show();
    }
    else if(value == 'Custom'){     
      jQuery('.schedule').remove();
      jQuery('.submit_value').remove();
      $(".custom").show();
    }
    else{
      jQuery('.schedule').remove();
      jQuery('.custom').remove();
      $(".submit_value").show();  
    }
  }
</script>

【问题讨论】:

  • ids 在文档中应该是唯一的。
  • @Teemu 你能详细说明一下吗
  • PHP foreach 循环使用硬编码的ids 创建trs 中的元素。当你引用像jQuery('#custom') 这样的元素时,jQuery 只会从文档中找到第一个带有id 的元素。
  • 详细说明。每个元素只能有一个唯一 ID,否则当您尝试执行以该 ID 为目标的脚本时会导致错误。不能有两个元素共享同一个 ID。
  • 您可以在此处阅读有关 html id 的更多信息:developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id

标签: javascript php jquery ajax


【解决方案1】:

使用下面的代码也许对你有帮助

<table id="mytable" class="table table-bordred table-striped">
  <thead>
    <th>NAME</th>
    <th>STATUS</th>
  </thead>
  <tbody>
    <?php foreach($student as $per_student): ?>
      <tr>
        <td><?php echo $per_student->name; ?></td>
        <td>
          <select class="can-dd status_value"  name="status">
            <option value="">STATUS</option>
            <option value="Accepted ">Accepted</option>
            <option value="Forwarded ">Forwarded </option>
            <option value="Schedule">Schedule </option>
            <option value="Custom">Custom</option> 
          </select>
          <div class="schedule input_box">
            <input  type="text" placeholder="schedule" name="schedule">
            <button type="submit" class="btn btn-primary "  >SUBMIT</button>
          </div>

          <div class="custom input_box">
            <input  type="text" placeholder="custom"  name="custom">
            <button type="submit" class="btn btn-primary "  >SUBMIT</button>
          </div>

          <div class="submit_value input_box">
            <button type="submit" class="btn btn-primary "  >SUBMIT</button>
          </div>
        </td>
      </tr>
    <?php endforeach; ?>
  </tbody>
</table>

<script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#mytable").on("change", ".status_value", function(){
        var selDiv = jQuery(this).parent("td");
        var selVal = jQuery(this).val();
        if(selVal == 'Schedule'){
            selDiv.find(".input_box").hide();
            selDiv.find(".schedule").show();
        } 
        else if(selVal == 'Custom'){
            selDiv.find(".input_box").hide();
            selDiv.find(".custom").show();
        } 
        else {
            selDiv.find(".input_box").hide();
            selDiv.find(".submit_value").show();
        }
    });
});
</script>

我必须从所有字段中删除 ID,并根据特定原始类设置 jQuery 更改事件

【讨论】:

    【解决方案2】:

    你可以试试这个,看看它是否适合你。我对代码进行了一些更改,但不是通过php 生成代码,而是通过JavaScript 生成代码,但应该不会有太大区别。

    在您的 html 文件中,您正在生成子 divs 共享相同 ID submit_value、custom、schedule 的行。您应该将这些 id 更改为动态生成或完全删除它们。我删除了它们,因为对我来说,它们似乎没有必要。

    这是一个修改后的 HTML 文件。

    <tr>
      <td>--- PHP GENERATED NAME ---</td>
      <td>
        <select class="can-dd" onchange="myFunction(event)" name="status">
          <option value="">STATUS</option>
          <option value="Accepted ">Accepted</option>
          <option value="Forwarded ">Forwarded </option>
          <option value="Schedule">Schedule </option>
          <option value="Custom">Custom</option>
        </select>
        <div class="schedule">
          <input  type="text" placeholder="schedule" name="schedule">
          <button type="submit" class="btn btn-primary "  >SUBMIT</button>
        </div>
    
        <div class="custom">
          <input  type="text" placeholder="custom"  name="custom">
          <button type="submit" class="btn btn-primary "  >SUBMIT</button>
        </div>
    
        <div class="submit_value">
          <button type="submit" class="btn btn-primary "  >SUBMIT</button>
        </div>
      </td>
    </tr>
    

    这是一个修改后的 javascript 代码。第一部分只是生成html,你可以跳过它。 myFunction 部分通过event.target 选择适当的元素,因此您可以针对文档的特定部分而不是整个文档。

    event.target 在您的情况下是 select 元素,因此如果您在其上调用 parentNode,您会得到相应的行。通过使用某个类名作为搜索查询对该行调用querySelector,您正在搜索具有该类的元素,但仅在当前行的子树内。

    // just to dynamically generate the table
    const generateTable = n => {
      const tBody = document.querySelector('tbody');
    
      for (let i = 0; i < n; i++) {
        let htmlCode = `<tr>
          <td>Some name</td>
          <td>
            <select class="can-dd" onchange="myFunction(event)" name="status">
              <option value="">STATUS</option>
              <option value="Accepted ">Accepted</option>
              <option value="Forwarded ">Forwarded </option>
              <option value="Schedule">Schedule </option>
              <option value="Custom">Custom</option>
            </select>
            <div class="schedule">
              <input  type="text" placeholder="schedule" name="schedule">
              <button type="submit" class="btn btn-primary "  >SUBMIT</button>
            </div>
    
            <div class="custom">
              <input  type="text" placeholder="custom"  name="custom">
              <button type="submit" class="btn btn-primary "  >SUBMIT</button>
            </div>
    
            <div class="submit_value">
              <button type="submit" class="btn btn-primary "  >SUBMIT</button>
            </div>
          </td>
        </tr>`
        tBody.innerHTML += htmlCode;
      }
    }
    
    function myFunction(e) {
      var value = e.target.value;
      if(value == 'Schedule'){
        e.target.parentNode.querySelector('.custom').remove();
        e.target.parentNode.querySelector('.submit_value').remove();
      }
      else if(value == 'Custom'){
        e.target.parentNode.querySelector('.schedule').remove();
        e.target.parentNode.querySelector('.submit_value').remove();
      }
      else{
        e.target.parentNode.querySelector('.schedule').remove();
        e.target.parentNode.querySelector('.custom').remove();
      }
    }
    
    generateTable(4);
    <table id="mytable" class="table table-bordred table-striped">
      <thead>
        <th>NAME</th>
        <th>STATUS</th>
      </thead>
      <tbody>
    
      </tbody>
    </table>

    【讨论】:

      【解决方案3】:

      我不是专家,仍处于学习阶段,但我想我可以尝试为此提供一个可能的解决方案。

      您提供的 id 名称如“Schedule”和“schedule”是有效的,因为它们在 HTML 中区分大小写。但是,在通过 CSS 调用这些函数时,它们被视为不区分大小写,如 https://www.w3.org/TR/selectors-3/#casesens 中所述

      因此,在通过 jQuery '#schedule' 使用 CSS #idname 时,可以考虑任何名为 id = schedule 或 Schedule 的标签。当有多个标签具有相同的 id 时,结果会因浏览器而异。参考:How jQuery works when there are multiple elements with the same "id"?

      尝试为所有元素提供唯一的 id 并尝试代码。它应该可以解决问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-07
        • 1970-01-01
        • 2016-05-08
        • 2021-12-28
        • 2023-02-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多