【问题标题】:Dynamically adding ID in js / jquery在js/jquery中动态添加ID
【发布时间】:2020-04-02 18:09:08
【问题描述】:

我有一个接受输入的表单,当单击添加按钮时,我使用脚本来复制我的表单。我的表单中有一个单选按钮,其中包含一个隐藏的文本区域。每当我选择 value="uygunDegil" 时,该文本区域都会变为可见。问题是当我附加我的复制表单时,我将无法使用我的功能访问我的复制单选按钮,假设我在我的第 5 个复制字段上选择value="uygunDegil",我的第一个隐藏文本区域变为可见而不是第 5 个。我需要添加某种 id 并将其动态地迭代到我的表单或我的单选按钮,但我不知道该怎么做。我是 JS 新手,所以任何帮助或建议将不胜感激。

Form.php

<form method="post">
<div class="form-group row">
  <div class="col-auto">
    <label for="ad">Ad</label>
    <input type="text" name="ad[]" class="form-control" id="ad" placeholder="Öğrencinin Adı"/>
    <label for="soyad">Soyad</label>
    <input type="text" name="soyad[]" class="form-control" id="soyad" placeholder="Öğrencinin Soyadı"/>
    <label for="no">No</label>
    <input type="text" name="numara[]" class="form-control" id="no" placeholder="Öğrencinin Numarası">
    <label for="course">Bölümü</label>
    <input type="text" name="bolum[]" class="form-control" id="course" placeholder="Öğrencinin Bölümü">
    <label for="alKredi">Almak İstediği Kredi</label>
    <input type="text" name="alKredi[]" class="form-control" id="alKredi" placeholder="Almak İstediği Kredi">
    <label for="verKredi">Alabileceği Kredi</label>
    <input type="text" name="verKredi[]" class="form-control" id="verKredi" placeholder="Alabileceği Kredi">
    <label for="evetKontrol">Evet</label>
    <input type="radio" id="evetKontrol" name="uygun" onclick="javascript:yesnoCheck();" value="uygun" checked>
    <label for="hayirKontrol">Hayır</label>
    <input type="radio" id="hayirKontrol" name="uygun" onclick="javascript:yesnoCheck();" value="uygunDegil">
    <div id="ifNo" style="visibility:hidden">
        <strong>Uygun Olmama Sebebi:</strong> <input type="textarea" id="hayirSebep" name="hayirSebep" style="height: 75px"><br>
    </div>
      <div class="input-group-addon">
          <a href="javascript:void(0)" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add</a>
      </div>
  </div>
</div>
<input type="submit" name="submit" class="btn btn-primary" value="SUBMIT"/>
</form>

我附加的表单副本

   <div class="form-group rowCopy" style="display: none;">
    <div class="col-auto">
      <label for="ad">Ad</label>
      <input type="text" name="ad[]" class="form-control" id="ad" placeholder="Öğrencinin Adı"/>
      <label for="soyad">Soyad</label>
      <input type="text" name="soyad[]" class="form-control" id="soyad" placeholder="Öğrencinin Soyadı"/>
      <label for="no">No</label>
      <input type="text" name="numara[]" class="form-control" id="no" placeholder="Öğrencinin Numarası">
      <label for="course">Bölümü</label>
      <input type="text" name="bolum[]" class="form-control" id="course" placeholder="Öğrencinin Bölümü">
      <label for="alKredi">Almak İstediği Kredi</label>
      <input type="text" name="alKredi[]" class="form-control" id="alKredi" placeholder="Almak İstediği Kredi">
      <label for="verKredi">Alabileceği Kredi</label>
      <input type="text" name="verKredi[]" class="form-control" id="verKredi" placeholder="Alabileceği Kredi">
      <?php  echo '<strong>Uygun mu?</strong><br><br>'; ?>
      <label for="evetKontrol">Evet</label>
      <input type="radio" id="evetKontrol" name="uygun" onclick="javascript:yesnoCheck();" value="uygun" checked>
      <label for="hayirKontrol">Hayır</label>
      <input type="radio" id="hayirKontrol" name="uygun" onclick="javascript:yesnoCheck();" value="uygunDegil">
      <div id="ifNo" style="visibility:hidden">
          <strong>Uygun Olmama Sebebi:</strong> <input type="textarea" id="hayirSebep" name="hayirSebep" style="height: 75px"><br>
      </div>
        <div class="input-group-addon">
            <a href="javascript:void(0)" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</a>
        </div>
    </div>
</div>

fields.js

$(document).ready(function() {
   //group add limit
   var maxGroup = 25;

   //add more fields group
   $(".addMore").click(function() {
      if ($('body').find('.row').length < maxGroup) {
         var fieldHTML = '<div class="form-group row">' + $(".rowCopy").html() + '</div>';
         $('body').find('.row:last').after(fieldHTML);
      } else {
         alert('Maximum ' + maxGroup + ' groups are allowed.');
      }
   });
   //remove fields group
   $("body").on("click", ".remove", function() {
      $(this).parents(".row").remove();
   });
});

function yesnoCheck() {
   if (document.getElementById('evetKontrol').checked) {
      document.getElementById('ifNo').style.visibility = 'hidden';
   } else document.getElementById('ifNo').style.visibility = 'visible';
}

JS fiddle to demostrate

【问题讨论】:

  • 使用data-id之类的属性并使用.data("id")获取它
  • 在 HTML 中,IDs 必须是 unique to the document,复制它们可能会导致问题。我建议改用类。
  • @showdev 这就是为什么我正在寻找一种方法来添加和迭代它们。如果不可能,我也可以上课。我的问题是我不知道是否可以在迭代时将一些属性与我的 js 代码添加到我的 html 表单中(buttonOne、buttonTwo、buttonThree 等)
  • 这真的是一个php问题吗?

标签: javascript jquery html function


【解决方案1】:

尝试添加var counter = 0;'&lt;div class="form-group row" id="radio' + counter +'"&gt;'

$(document).ready(function() {
       //group add limit
       var maxGroup = 25;
       var counter = 0;


       //add more fields group
       $(".addMore").click(function() {
          if ($('body').find('.row').length < maxGroup) {
             var fieldHTML = '<div class="form-group row" id="radio' + counter +'">' + $(".rowCopy").html() + '</div>';
             counter +=1;
             $('body').find('.row:last').after(fieldHTML);
          } else {
             alert('Maximum ' + maxGroup + ' groups are allowed.');
          }
       });

或者没有计数器的简单类&lt;div class="form-group row" class="myRadio"&gt;

【讨论】:

    【解决方案2】:

    将“ifNo”切换为一个类——ID 必须是唯一的才能正常工作——然后将其放入您的 yesnoCheck:

    $(this).parent().find('.ifNo').css('visibility','visible')

    父级(单数只能上一级!)将您置于 .col-auto 中,因此您仍在行内,并且 find 只会找到您想要的“ifNo”。

    对于这么简单的事情来说,这已经足够好了。但是对于任何远程复杂的事情,我都会使用 IronCat 发布的相同想法:生成唯一的行 ID 并使用它们进行导航。

    【讨论】:

      【解决方案3】:

      根据 IronCat 提供的信息,我设法找到了解决问题的方法。稍微更改了我的代码。希望对像我这样的新手有用。

      $(function() {
      
        function addElement(tObj) {
          var counter = $("[id^='ogrenci']", tObj).length;
          var html = '<div class="col-auto" id="ogrenci_' + counter + '"><label for="ad">Ad</label><input type="text" name="ad[]" class="form-control" id="ad_' + counter + '" placeholder="Öğrencinin Adı"/><label for="soyad">Soyad</label><input type="text" name="soyad[]" class="form-control" id="soyad_' + counter + '" placeholder="Öğrencinin Soyadı"/><label for="no">No</label><input type="text" name="numara[]" class="form-control" id="no_' + counter + '" placeholder="Öğrencinin Numarası"><label for="course">Bölümü</label><input type="text" name="bolum[]" class="form-control" id="course_' + counter + '" placeholder="Öğrencinin Bölümü"><label for="alKredi">Almak İstediği Kredi</label><input type="text" name="alKredi[]" class="form-control" id="alKredi_' + counter + '" placeholder="Almak İstediği Kredi"><label for="verKredi">Alabileceği Kredi</label><input type="text" name="verKredi[]" class="form-control" id="verKredi_' + counter + '" placeholder="Alabileceği Kredi"><label for=""><strong>Uygun mu?</strong> </label><br><label for="evetKontrol_' + counter + '">Evet</label><input type="radio" id="evetKontrol_' + counter + '" name="uygun_' + counter + '" value="uygun" checked><label for="hayirKontrol_' + counter + '">Hayır</label><input type="radio" id="hayirKontrol_' + counter + '" name="uygun_' + counter + '" value="uygunDegil"><div id="ifNo_' + counter + '" style="visibility:hidden"><strong>Uygun Olmama Sebebi:</strong> <input type="textarea" id="hayirSebep_' + counter + '" name="hayirSebep" style="height: 75px"><br></div><div class="input-group-addon"><a href="#" id="remove_' + counter + '" class="btn btn-danger remove"><span class="glyphicon glyphicon glyphicon-remove" aria-hidden="true"></span> Remove</a></div></div>';
          tObj.append(html);
        }
      
        function showHidden() {
          $("[id^='hayirKontrol']").each(function(i, el) {
            var rel = $("#ifNo_" + i);
            if ($(el).is(":checked")) {
              rel.css("visibility", "visible");
            } else {
              rel.css("visibility", "hidden");
            }
          });
        }
      
        //add more fields group
        $("#add").click(function() {
          addElement($("#container"));
        });
      
        //remove fields group
        $('#container').on('click', "a[id^='remove']", function() {
          $(this).parents('div.col-auto').remove();
        });
      
        $("#container").on("change, click", "input[type='radio']", showHidden);
        showHidden();
      });
      <!DOCTYPE html>
      <html>
        <head>
          <title>Fazla Kredi</title>
          <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
          <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
          <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js"></script>
          <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" ></script>
          <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" ></script>
        </head>
        <body>
      
          <form method="post">
            <div class="form-group row" id="container">
              <div class="col-auto" id="ogrenci_0">
                <label for="ad">Ad</label>
                <input type="text" name="ad[]" class="form-control" id="ad_0" placeholder="Öğrencinin Adı" />
                <label for="soyad">Soyad</label>
                <input type="text" name="soyad[]" class="form-control" id="soyad_0" placeholder="Öğrencinin Soyadı" />
                <label for="no">No</label>
                <input type="text" name="numara[]" class="form-control" id="no_0" placeholder="Öğrencinin Numarası">
                <label for="course">Bölümü</label>
                <input type="text" name="bolum[]" class="form-control" id="course_0" placeholder="Öğrencinin Bölümü">
                <label for="alKredi">Almak İstediği Kredi</label>
                <input type="text" name="alKredi[]" class="form-control" id="alKredi_0" placeholder="Almak İstediği Kredi">
                <label for="verKredi">Alabileceği Kredi</label>
                <input type="text" name="verKredi[]" class="form-control" id="verKredi_0" placeholder="Alabileceği Kredi">
                <label for=""><strong>Uygun mu?</strong> </label><br>
                <label for="evetKontrol_0">Evet</label>
                <input type="radio" id="evetKontrol_0" name="uygun_0" value="uygun" checked>
                <label for="hayirKontrol_0">Hayır</label>
                <input type="radio" id="hayirKontrol_0" name="uygun_0" value="uygunDegil">
                <div id="ifNo_0" style="visibility:hidden">
                  <strong>Uygun Olmama Sebebi:</strong> <input type="textarea" id="hayirSebep_0" name="hayirSebep" style="height: 75px"><br>
                </div>
                <div class="input-group-addon">
                  <a href="javascript:void(0)" id="add" class="btn btn-success addMore"><span class="glyphicon glyphicon glyphicon-plus" aria-hidden="true"></span> Add</a>
                </div>
              </div>
            </div>
            <input type="submit" name="submit" class="btn btn-primary" value="SUBMIT" />
          </form>
      <script src="fields.js"></script>
        </body>
      </html>

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-01-09
        • 2017-04-28
        • 2013-04-28
        • 2014-03-09
        • 1970-01-01
        • 1970-01-01
        • 2010-12-12
        • 1970-01-01
        相关资源
        最近更新 更多