Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用动态生成的链接动态更新html_Javascript_Node.js_Angularjs_Express - Fatal编程技术网

Javascript 使用动态生成的链接动态更新html

Javascript 使用动态生成的链接动态更新html,javascript,node.js,angularjs,express,Javascript,Node.js,Angularjs,Express,我的应用程序生成一个单词列表,并将这些单词转换为链接。我希望用户能够点击这些链接,并使用该词作为搜索词。如何从HTML链接中检索{{word.name}}?我不希望它生成一个新的页面,如果我点击链接。我只想能够检索链接中的文本 HTML: Node.js服务器代码: app.get('*', function(req, res) { //get the {{ word.name }} from the HTML and use it to search //s

我的应用程序生成一个单词列表,并将这些单词转换为链接。我希望用户能够点击这些链接,并使用该词作为搜索词。如何从HTML链接中检索{{word.name}}?我不希望它生成一个新的页面,如果我点击链接。我只想能够检索链接中的文本

HTML:

Node.js服务器代码:

  app.get('*', function(req, res) 
  {
      //get the {{ word.name }} from the HTML and use it to search
      //sql = select.....+word.name;
      connection.query(sql, function (err, result, fields) 
      {
         if(err) throw err;
         res.json(result);
      });
  });

您需要在表单的post数据中添加单词

当您发出post请求时,您会在服务器上发布一些数据。 您通常会执行以下操作:

<form action="yourfile.php" method="post">
    <input type="text" name="key_word" value="Search a word ...">
    <input type="submit" value="Search">
</form>
这将向yourfile.php发布数据关键字=输入值

要使用javascript执行此操作,请使用:

<form id="myForm" action="yourfile.php" method="post">
   <input type="text" id="key_word" name="key_word" value="Search a word ...">
   <input type="submit" value="Search">
</form>
<script>
   function postMe(word){
      document.getElementById("key_word").value=word;
      document.getElementById("myForm").submit();
   }
</script>

<a onclick="postMe(this.innerHTML)">yourWord1Here</a>
<a onclick="postMe(this.innerHTML)">yourWord2Here</a>
<a onclick="postMe(this.innerHTML)">yourWord3Here</a>
...
示例:

您可以尝试

<li>
      <a href="#" ng-click="searchWord=word.name">{{ word.name }}</a> 
</li>

我不知道你的意思。你能澄清一下吗?但这是从html方面来的。必须在服务器上检索数据。
<form id="myForm" action="yourfile.php" method="post">
   <input type="text" id="key_word" name="key_word" value="Search a word ...">
   <input type="submit" value="Search">
</form>
<script>
   function postMe(word){
      document.getElementById("key_word").value=word;
      document.getElementById("myForm").submit();
   }
</script>

<a onclick="postMe(this.innerHTML)">yourWord1Here</a>
<a onclick="postMe(this.innerHTML)">yourWord2Here</a>
<a onclick="postMe(this.innerHTML)">yourWord3Here</a>
...
<li>
      <a href="#" ng-click="searchWord=word.name">{{ word.name }}</a> 
</li>
$scope.search = function() {
    $http.post('*', $scope.searchWord)
        .success(function(data) {
            $scope.formData     = {}; 
            $scope.word        = data;
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};