简体   繁体   English

JS - 更改特定文本输入字段的值(类有几个输入元素)

[英]JS - Change value of specific text-input-field (class has several input elements)

I have this JS function:我有这个 JS function:

 function insert(i) {
      let elem = document.querySelector('.input_user');
      let old = elem.value;
      elem.value = old + i;
    };

I want to let the user add special characters to the input field.我想让用户在输入字段中添加特殊字符。 The problem is that there are several input fields with the same class since the form is generated by a php loop.问题是有几个输入字段具有相同的 class 因为表单是由 php 循环生成的。 So it will always put the char in the first input field generated.所以它总是将 char 放在生成的第一个输入字段中。

<form action="ergebnisse.php" method="post">
          <?php
          while($row = $abfrage->fetch_assoc()) { ?>
            <div class="abfragefeld grid-8">
              <p class="vokabel"><?php echo $row["art_de"] . ' ' . $row["vok_de"]?></p>
              <p class="form"><?php echo $row["form"] ?></p>
              <p id="beispiel"><?php echo $row["beispiel"] ?></p>
              <input type="hidden" value="<?php echo $row['id']?>" name="id[]">
              <select class="art_select" name="art_fr_ip[]">
                <option value="le">le</option>
                <option value="la">la</option>
                <option value="l'">l&apos;</option>
                <option value="les">les</option>
                <option value="Ils/Elles">Ils/Elles</option>
                <option value="">kein Artikel</option>
              </select>
              <input class="input_user" type="text" name="vok_fr_ip[]">
              <div class="special_chars">
                <a onclick="insert('Ç')" class="char">&Ccedil;</a>
                <a onclick="insert('ç')" class="char">&ccedil;</a>
              </div>
            </div>
            <?php
          }
        } else {
          echo "0 results";
        }?>
      </div>

      <div class="slide-nav left_nav">
        <a type="button" class="prev" onclick="plusSlides(-1)">&#8249;</a>
      </div>
      <div class="slide-nav right_nav">
        <a class="next" onclick="plusSlides(1)">&#8250;</a>
      </div>
      <div class="submit_abfrage">
        <input class="check" type="submit" value="Alle Antworten abschicken">
      </div>
    </form>

Is there a way to tell JS to send the string to the active input field or to the one with a certain CSS value (one only at a time is visible, others set to display: none)?有没有办法告诉 JS 将字符串发送到活动输入字段或具有特定 CSS 值的字符串(一次只有一个可见,其他设置为显示:无)?

Thank You谢谢你

如何选择活动输入字段

First step would be to avoid inline handlers, since they have terrible scoping rules and have escaping problems - better to use addEventListener nowadays.第一步是避免内联处理程序,因为它们有糟糕的范围规则并且有 escaping 问题 - 现在最好使用addEventListener

It looks like the character you want to insert is the same as the textContent of the <a> being clicked, so you can use that instead of passing an argument to insert .看起来您要插入的字符与被单击的<a>textContent相同,因此您可以使用它而不是将参数传递给insert

On click of something in a special_chars div, go to the upper div and then to the previous sibling to get to the <input> , and you can change it for that row.单击special_chars div 中的某些内容时,go 到上部 div,然后到上一个兄弟以到达<input> ,您可以为该行更改它。

document.querySelector('form').addEventListener('click', (e) => {
  if (!e.target.closest('.special_chars') || !e.target.matches('a')) return;
  // Then the click was on an `<a>` we're interested in:
  const charToInsert = e.target.textContent;
  const input = e.target.parentElement.previousElementSibling;
  input.value += charToInsert;
});

Another option, by iterating over all .special_chars elements:另一种选择,通过遍历所有.special_chars元素:

for (const container of document.querySelectorAll('.special_chars')) {
  container.addEventListener('click', (e) => {
    if (e.target.matches('a')) {
      container.previousElementSibling.value += e.target.textContent;
    }
  });
}

Generally, form inputs will have a name attribute.通常,表单输入将具有name属性。 CSS selectors don't have to be the class. CSS 选择器不必是 class。 There are several forms: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors In this case, what you're looking for is "Attribute Selector"有几个 forms: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors在这种情况下,您正在寻找的是“属性选择器”

Without knowing which specific one you want, the general pattern would be:在不知道您想要哪个特定的情况下,一般模式是:

document.querySelector('input[name="field-name"]')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM