如何创建“搜索”字段的副本?

时间:2018-12-27 23:31:05

标签: javascript jquery clone

我的应用程序中有一个搜索字段。我需要使用相同的功能克隆此搜索字段。页面左侧的一个搜索字段,页面右侧的另一个搜索字段。

如何使用JS进行克隆?

在我的JS代码下面

AuthenticationSchemes

和部分HTML代码

declare @MyTable table (F varchar(3), G varchar(3), id int)

insert into @MyTable (F, G, id)
    select '123' [F], 'ABC' [G], 1 id union
    select 'ABC' [F], '123' [G], 2 id union
    select 'DEF' [F], '123' [G], 3 id

SELECT A.F, A.G
FROM @MyTable A
where not exists (select 1 from @MyTable B where A.F = B.G and A.G = B.F and A.id < B.id)
GROUP BY A.F, A.G

2 个答案:

答案 0 :(得分:0)

作为一种选择,我建议创建一些函数或类来创建输入和描述节点,并将其附加到传递给它的任何容器ID(或类)上。在html中,您只需要具有rowDiv id的元素。显然,您可以根据自己的需要进行调整,但这只是一个想法。 我认为是这样的:

// rough example based of your code
class citySearch {
    constructor(parentContainerId) {
        this.searchField;
        this.displayArea;
        this.setStage(parentContainerId);
        this.hookListener();
    }

    setStage(parentContainerId) {
        this.searchField = document.createElement('input');
        this.displayArea = document.createElement('div');
        var parentContainer = document.getElementById(parentContainerId)

        parentContainer.appendChild(this.searchField);
        parentContainer.appendChild(this.displayArea);
    }

    show(data) {
        return "<h2>Current Weather for " + data.name + "," + data.sys.country + "</h2>" +
            "<h3><strong>Wind Speed</strong>: " + data.dt + "</h3>" +
            "<h3><strong>Weather</strong>: <img src='http://openweathermap.org/img/w/" + data.weather[0].icon + ".png'>" + data.weather[0].main + "</h3>" +
            "<h3><strong>Description</strong>: " + data.weather[0].description + "</h3>" +
            "<h3><strong>Temperature</strong>: " + data.main.temp + "&deg;C</h3>" +
            "<h3><strong>Wind Direction</strong>: " + data.wind.deg + "&deg;</h3>";

    }

    hookListener() {
        this.searchField.addEventListener('keypress', this.onClick.bind(this));
    }

    onClick(e) {
        if (e.keyCode === 13) {
            var city = this.searchField.value;
            if (city !== '') {
                fetch('http://api.openweathermap.org/data/2.5/weather?q=' + city + "&units=metric" + "&APPID=bb037310921af67f24ba53f2bad48b1d")
                .then( async(res) => {
                    const data = await res.json();
                    var widget = this.show(data);
                    this.displayArea.innerHTML = widget;
                    this.searchField.value = '';
                })

            } else {
            this.displayArea.innerHTML = "<div class='alert alert-danger text-center'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>Field cannot be empty</div>";
            }
        }
    }
}
var firstSearch = new citySearch('rowDiv');
var secondSearch = new citySearch('rowDiv');

HTML

 <div class="row form-group form-inline" id="rowDiv"></div>

答案 1 :(得分:0)

这里有一些示例代码,该代码允许1个代码库,2个搜索框并使两个搜索框保持同步。

const searchBoxes = () => document.getElementsByClassName('searchbox');

document.addEventListener('DOMContentLoaded', function() {

  Array.from(searchBoxes()).forEach(element => {
    console.log(element.id);
    element.addEventListener('keyup', function(event) {
      let text = event.target.value;
      // This is just a demo
      document.getElementById("searchResult").innerHTML = text;
      // Loop other search boxes
      Array.from(searchBoxes()).forEach(e => {
        if (e.id != event.target.id) e.value = text;
      });

      // ANY CODE HERE TO APPLY SEARCH
    });
  });

});
.searchbox {
  border: 1px solid black;
  background-color: wheat;
}

#searchResult {
  height: 100px;
  width: 100px;
  background-color: yellow;
  font-weight: bold;
}
<div>
  <span>Some Text Here</span>
  <input id="search1" class="searchbox" type="text" />
</div>
<div id="searchResult">ALL MY PAGE STUFF</div>
<div>
  <span>Some Text Here</span>
  <input id="search2" class="searchbox" type="text" />
</div>