如何在第一个下拉列表发生变化时填充2个文本框和1个下拉列表

时间:2019-03-28 17:56:50

标签: javascript html

我试图在文本框中显示星系的坐标,并根据第一个下拉菜单的结果填充一个下拉菜单。我可以使用链接的下拉列表,但无法确定如何填充相关文本框

我找到的答案可以回答一个问题或另一个问题,但不能同时解决两个问题,将我找到的答案组合都不能解决问题

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 

<div class="form-group"><div class="row"><div class="col"><label for="target-list">Target Name:</label></div></div> <select id="choices" type="select" name="name" placeholder="Target Name" class="button form-control" aria-required="true" aria-invalid="false"></select>
<div class="alert alert-danger" style="display: none;"></div></div> <div class="form-group"><label>Right Ascension:</label> <input id="Right Ascension" type="text" placeholder="HH:MM:SS.S" name="ra" class="form-control" aria-required="true" aria-invalid="false"> <div class="alert alert-danger" style="display: none;"></div></div> <div class="form-group"><label>Declination:</label> <input type="text" placeholder="DD:MM:SS.S" name="dec" class="form-control" aria-required="true" aria-invalid="false"> <div class="alert alert-danger" style="display: none;"></div></div>

 <div><label class="wrapper" for="states">Filter</label>

$(document).ready(function() {
  //init data
  var arrayList = [{
      "Id": 0,
      "Name": "Select a target...",
      "Right Ascension": "HH:MM:SS.S",
      "Declination":"DD:MM:SS.S"
    },
    {
      "Id": 1,
      "Name": "M15",
        "Right Ascension": "21:29:58.3",
      "Declination":"+12:10:01.2"
    },
    {
      "Id": 2,
      "Name": "M27"

    },
    {
      "Id": 3,
      "Name": "NGC891"
    }

  ];


  for (var i = 0; i <= arrayList.length; i++) {
    $('#choices').append('<option value="' + arrayList[i].Id + '">' + arrayList[i].Name + '</option>');
  }


});

$("#choices").change(function() {
  // $.getJSON("jsondata/data.json", function(data) {
  //use this if using external json sets

  var $selection = $("#choices option:selected");
  var key = $selection.val();

  var vals = [];

  $(".imghere").attr("src", "");

  switch (key) {
    case '1':
      text: ["Please Select a Filter","Red (Rp)", "Green (V)", "Blue (B)"],
      vals = ["Please Select a Filter...","Red", "Green", "Blue"]; 
      break;
    case '2':
      vals = ["Please Select a Filter...","Red (Rp)", "Green (V)", "Blue (B)"] //data.Berkeley 73.split(",");
      break;
    case '3':
      vals = ["Please Select a Filter...","Red (Rp)", "Green (V)", "Blue (B)"] //data.Czernic 27.split(",");
      break;
    case '0':
      vals = ['Please Select a Filter...'];
  }


  var $secondChoice = $("#filter");
  $secondChoice.empty();
  $.each(vals, function(index, value) {
    $secondChoice.append("<option>" + value + "</option>");
  });

});


$("#filter").change(function() {
  var $filter = $("#filter option:selected").val();
  var baseurl = "https://github.com/brychanjames/RTI-Simulator/blob/master/";
  var $showimg = baseurl + $filter + ".jpg";
  //$(".imghere").attr("src",$showimg); use this, the next line is demo purposes
  $(".imghere").attr("src", $showimg); /*use variable in place of the placeholder image*/


});

我希望能够显示目标的右上角和下偏,并使用显示的值填充下拉菜单,我创建了一个小提琴,其中显示了选择框,其中的文本框为惰性。

[http://jsfiddle.net/brychang/jtf8k5ym/33/]

1 个答案:

答案 0 :(得分:1)

我能够做到,但是无需从数组中打印名称,这意味着您必须通过更改jquery代码中的'test'和'test2'来自行填充值。

更改:

<input id="Right Ascension" type="text" placeholder="HH:MM:SS.S" name="ra" class="form-control" aria-required="true" aria-invalid="false">
<input type="text" placeholder="DD:MM:SS.S" name="dec" class="form-control" aria-required="true" aria-invalid="false">

<input id="right" type="text" placeholder="HH:MM:SS.S" name="ra" class="form-control" aria-required="true" aria-invalid="false">
<input id="desc" type="text" placeholder="DD:MM:SS.S" name="dec" class="form-control" aria-required="true" aria-invalid="false">

JQUERY: 将值“ test”和“ test2”修改为任意值

  switch (key) {
    case '1':
      text: ["Please Select a Filter","Red (Rp)", "Green (V)", "Blue (B)"],
      vals = ["Please Select a Filter...","Red", "Green", "Blue"]; 
      $('#right').val('test');
      $('#desc').val('test2');
      break;
    case '2':
      vals = ["Please Select a Filter...","Red (Rp)", "Green (V)", "Blue (B)"] //data.Berkeley 73.split(",");
      $('#right').val('test');
      $('#desc').val('test2');
      break;
    case '3':
      vals = ["Please Select a Filter...","Red (Rp)", "Green (V)", "Blue (B)"] //data.Czernic 27.split(",");
      $('#right').val('test');
      $('#desc').val('test2');
      break;
    case '0':
      vals = ['Please Select a Filter...'];
  }