简体   繁体   English

使用javascript填充两个单独的输入字段,其中包含由多个按钮传入的唯一文本值

[英]Populating two separate input fields with unique text values passed in by multiple buttons using javascript

Working on a small personal project with jQuery and JavaScript. 使用jQuery和JavaScript处理一个小型个人项目。 This is a one page app on a static website. 这是静态网站上的单页应用。

The game (if you can call it that) asks the user to select 2 characters from the selection area and pressing enter transfers the input to the "battle-area" where when you hit the "fight" button random numbers are subtracted form the characters hitpoints as attack dmg. 游戏(如果你可以称之为)要求用户从选择区域中选择2个字符并按Enter键将输入传输到“战区”,当你点击“战斗”按钮时,从字符中减去随机数字作为攻击dmg的生命值。

I am stuck on the selection part. 我被困在选择部分。 I need two separate input fields populated with unique names. 我需要两个单独的输入字段填充唯一的名称。 I am able to write text into the field and transfer it but I want the input fields to be populated by clicking on the individual pictures on the screen. 我能够将文本写入字段并传输它但我希望通过单击屏幕上的各个图片来填充输入字段。 My code seems to populate both fields at once. 我的代码似乎一次填充两个字段。 I can hardcode two buttons to each populate one input field, the problem is that I have about 15 image buttons and I need to be able to click either of those to populate the fields. 我可以硬编码两个按钮,每个按钮填充一个输入字段,问题是我有大约15个图像按钮,我需要能够点击其中任何一个来填充字段。

function selectFighter(name) {


  var x = name;

  var input = $('#fighter1_name').val();

  $('#fighter1_name').val(x);

  if ($('#fighter1_name').val() != "") {
    $('#fighter2_name').val(x);
  }
}

I have tried to add a condition that checks the first input field for a value and if it is NOT empty, to instead write the input into the second field. 我试图添加一个检查值的第一个输入字段的条件,如果它不为空,则将输入写入第二个字段。 But that is causing one button click to populate both fields. 但这导致单击一次按钮来填充这两个字段。 I need one button click to populate one field, and then when I click another button to populate the other, empty field. 我需要一个按钮单击以填充一个字段,然后当我单击另一个按钮以填充另一个空字段时。

I am passing in a name with an onclick event: onclick="selectFighter('bob');" 我正在传递一个带有onclick事件的名字:onclick =“selectFighter('bob');”

Problem is when I click the image, both fields are populated with the same name. 问题是当我单击图像时,两个字段都填充了相同的名称。 I want to be able to click an image to populate one input field, and then click a different image and put that name in the input field. 我希望能够单击图像以填充一个输入字段,然后单击其他图像并将该名称放在输入字段中。

This should do the trick. 这应该可以解决问题。 We're checking if the first field has a value - if so, we populate the second one - otherwise the first one. 我们正在检查第一个字段是否有值 - 如果是,我们填充第二个字段 - 否则是第一个字段。

function selectFighter(name) {
  if ($('#fighter1_name').val()) {
    $('#fighter2_name').val(name);
  } else {
    $('#fighter1_name').val(name);
  }
}

Add a class to the elements you click, and an ID, and remove all inline javascript. 将类添加到您单击的元素和ID,并删除所有内联JavaScript。

Then add a script tag with the event handler targeting the elements 然后使用针对元素的事件处理程序添加脚本标记

 $('.toBeClicked').on('click', function() { var x = $(this).data('name'); // note that "name" is not a good name for a variable var f1 = $('#fighter1_name'); var f2 = $('#fighter2_name'); f1.val() === "" ? f1.val(x) : f2.val(x); }); $('#clear').on('click', function() { $('input[id^=fighter]').val("") }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <button class="toBeClicked" data-name="bob">Bob</button> <button class="toBeClicked" data-name="jon">Jon</button> <button class="toBeClicked" data-name="ann">Ann</button> <br><br> Fighter 1 <input id="fighter1_name"><br> Fighter 2 <input id="fighter2_name"> <br><br> <button id="clear">Clear</button> 

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

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