Warm tip: This article is reproduced from serverfault.com, please click

javascript-使用单选将文本复制到字段中

(javascript - copy text into field using radio selection)

发布于 2020-11-29 02:10:32

我想使用CSS,HTML和JavaScript创建以下内容

Course1 //下拉选择// .... Course2 //下拉选择// ..... WINNER(选中“ RADIO”,选择“ Course1”)或(选中“ RADIO”,单击“ Curs2”) //

但是我的下拉菜单选择和单选按钮互相妨碍。

当我从广播中获得名称时,可以选择相同的“优胜者”广播,但是从Course1或Course2复制不起作用。

也许有人在其他地方创建了这样的代码,并且知道如何解决它?

任何帮助将不胜感激。

代码如下:

    <!--Make sure the form has the autocomplete function switched off:-->
    <form autocomplete="off" action="/action_page.php">
    <div class="autocomplete" style="width:300px;">
    Course 1
    <input id="myInput" type="text" name="golfcoursename1" placeholder="Golf 
    Course">
    <form autocomplete="off" action="/action_page.php">
    <div class="autocomplete" style="width:300px;">
    Course 2
    <input id="myInput1" type="text" name="golfcoursename2" placeholder="Golf 
    Course">
    </div>
    <p>
    WINNER
    <p>
    <input type="radio" id="Course1" name="winnerselected" value="Course1" 
    onclick="FillWinner(this.form)">
    <label for="Course1">Course 1</label>  
    <input type="radio" id="Course2" name="winnerselected" value="Course2" 
    onclick="FillWinner2(this.form)">
    <label for="Course2">Course 2</label><br>
    <input type="text" id="winner" name="Winner" placeholder="Winner">
        <p>
    </p>  
    <input type="submit">
    </form>

    <script>
    function FillWinner(f) {
    if(f.winnerselected.checked == true) {
    f.winner.value = f.golfcoursename1.value;
    if(f.winnerselected.checked == true) 
    f.winner.value = f.golfcoursename2.value;
    }}
    </script>
Questioner
Blakey
Viewed
11
Scott Marcus 2020-11-29 23:56:06

首先,你的HTML无效,因为你的第二个HTML无效form,嵌套在第一个中,没有结束标记。同样,虽然不关闭p元素是合法的,但为清楚起见,你确实应该这样做。

接下来,从HTML中删除内联样式和内联JavaScript。它只会使代码混乱,导致冗余,并且难以阅读和维护。而是将你的工作分为HTML,CSS和JavaScript部分。

目前尚不清楚你到底想要什么,但是我的猜测是,无论单击哪个单选按钮,都应该决定哪个文本框值成为赢家。基于此,请参见下面的内联注释,以获取有关代码工作方式的描述。

.autocomplete { width:300px; }
<!--Make sure the form has the autocomplete function switched off:-->
<form autocomplete="off" action="/action_page.php">
  <div class="courses">
    <div class="autocomplete">
      Course 1 <input id="myInput" name="golfcoursename1" placeholder="Golf Course">
    </div>
    <div class="autocomplete">
      Course 2 <input id="myInput1" name="golfcoursename2" placeholder="Golf Course">
    </div>
  </div>
  <p>WINNER</p>
  <p id="radioContainer">
    <input type="radio" id="Course1" name="winnerselected" value="Course1">
    <label for="Course1">Course 1</label>  
    <input type="radio" id="Course2" name="winnerselected" value="Course2">
    <label for="Course2">Course 2</label><br>
    <input type="text" id="winner" name="Winner" placeholder="Winner">
  </p>  
  <input type="submit">
</form>

<script>
  // Don't use inline HTML event attributes like onclick.
  // Separate your JavaScript from your HTML
  
  // Get references to the element(s) you'll need to work with
  
  // Get all the elements that have a name attribute that starts with "golfcoursename"
  const courseNames = document.querySelectorAll("[name^='golfcoursename']");
  
  // Get all the elements that have a name attribute that is exactly "winnerselected"
  const radioButtons = document.querySelectorAll("[name='winnerselected']");
  const winner = document.getElementById("winner");
  
  // Here's how to set up events in JS
  const radCont = document.getElementById("radioContainer").addEventListener("click", fillWinner);
  
  function fillWinner(event) {
    // Look at the radiobuttons collection and get the index of the selected radio button from it.
    const indexOfTextbox = Array.from(radioButtons).indexOf(event.target);
   
    // Set the value of the winner textbox to textbox with the same index as the clicked radio button
    winner.value = courseNames[indexOfTextbox].value;
  }
</script>