简体   繁体   English

使用Jquery附加到href标签吗?

[英]Append to href tags using Jquery?

I am learning to use Jquery and I am able to fetch data from the db inside a servlet and then send the json array to my ajax function. 我正在学习使用Jquery,并且能够从servlet内的db中获取数据,然后将json数组发送到我的ajax函数。 Now inside my ajax function, I am able to extract the values and my goal is to add the data to my href tags which is inside my tag. 现在,在我的ajax函数中,我能够提取值,并且我的目标是将数据添加到我的标签中的href标签中。 Now, I am a bit confused about something- I want to create href link dynamically and append the values from my ajax to the href links. 现在,我对某些事情有些困惑-我想动态创建href链接,并将ajax中的值附加到href链接。 How do I go about it ? 我该怎么办?

 $(document).on("click", "#check", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function... //event.preventDefault(); $.ajax({ type: "GET", url: "HomeServlet", success: function(data) { console.log(data); $.each(data, function(key, value) { console.log(value.value1); //alerting the values set in the JSONObject of the Sevlet. console.log(value.value2); }) }, //dataType: "json", contentType: "application/json" }); return false; }); 
 .vertical-menu { width: 200px; } .vertical-menu a { background-color: #eee; color: black; display: block; padding: 12px; text-decoration: none; } .vertical-menu a:hover { background-color: #ccc; } .vertical-menu a.active { background-color: #4CAF50; color: white; } 
 <form> <div class="vertical-menu"> <a href="#" class="active">Home</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 4</a> </div> API Name:<br> <input type="text" id="apiname" name="apiname"> API ENDPOINT:<br> <input type="text" id="apiendpoint" name="apiendpoint"> <br> API VERSION:<br> <input type="text" id="apiversion" name="apiversion"> ACCESSIBLE: <br> <input type="checkbox" name="source" value="internet"> Internet<br> <input type="checkbox" name="source" value="vpn"> VPN<br> <!-- <br><br> <input type="submit" formaction="Home" method="post" value="Submit"> --> <br> <input type="submit" id="check" name="check" value="Check"> </form> <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 

you can use += which essentially adds to the current value. 您可以使用+= ,这实际上会增加当前值。 Eg 例如

var one = 'string one',
    two = 'string two';

one += ' is better than ';

console.log(one); //this will return string one is better than.

using += takes the current value then add what's specified. 使用+=获取当前值,然后添加指定的值。 So in your case, let's say your data return looks like this: 因此,在您的情况下,假设您的数据返回如下所示:

var array = ['string', 'one', 'string', 'two'],
    myStr = '';

array.each(function(i, v)
{
    myStr += v;
})

console.log(myStr) // will return stringonestringtwo

hope this makes sense :) 希望这是有道理的:)

So the best way to do this is just make a div and give it an id that you want the code appended too and just access it's object in jquery. 因此,执行此操作的最佳方法是制作一个div并为其提供一个ID(您也要附加代码),然后在jquery中访问它的对象。 Ex: 例如:

Html HTML

    <div id="idtoappendto" class="container"></div>

Javascript - Jquery Javascript-jQuery

     $('#idtoappendto').html = `<a href="#data">Generated Html</a>`;
$(document).on("click", "#check", function() { // When HTML DOM "click" 
event is invoked on element with ID "somebutton", execute the following 
function...
//event.preventDefault();
  $.ajax({
     type: "GET",
     url: "HomeServlet",
     success: function(data) {
     console.log(data);
      $.each(data, function(key, value) {
        $(".vertical-menu").append("<a href='#"+value.value1+"'>"+value.value2+"</a>")
  })


},
//dataType: "json",
contentType: "application/json"
 });
return false;
});

Here you go with the solution https://jsfiddle.net/o2j5q880/ 在这里,您可以使用解决方案https://jsfiddle.net/o2j5q880/

 $(document).on("click", "#check", function() { // When HTML DOM "click" event is invoked on element with ID "somebutton", execute the following function... //event.preventDefault(); $.ajax({ type: "GET", url: "HomeServlet", success: function(data) { console.log(data); $.each(data, function(key, value) { console.log(value.value1); //alerting the values set in the JSONObject of the Sevlet. console.log(value.value2); }) }, //dataType: "json", contentType: "application/json" }); return false; }); // -------- Example code --------------- var data = [{ value1: "http://www.google.com", value2: "Google" },{ value1: "http://www.yahoo.com", value2: "Yahoo" },{ value1: "https://jsfiddle.net", value2: "JSFiddle" },{ value1: "https://stackoverflow.com", value2: "Stackoverflow" },{ value1: "https://codepen.io/", value2: "CodePen" }]; var verticalMenu = $(".vertical-menu").children(); $.each(data, function(key, value) { $(verticalMenu[key]).attr('href', value.value1).text(value.value2); }); 
 .vertical-menu { width: 200px; } .vertical-menu a { background-color: #eee; color: black; display: block; padding: 12px; text-decoration: none; } .vertical-menu a:hover { background-color: #ccc; } .vertical-menu a.active { background-color: #4CAF50; color: white; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form> <div class="vertical-menu"> <a href="#" class="active">Home</a> <a href="#">Link 1</a> <a href="#">Link 2</a> <a href="#">Link 3</a> <a href="#">Link 4</a> </div> API Name:<br> <input type="text" id="apiname" name="apiname"> API ENDPOINT:<br> <input type="text" id="apiendpoint" name="apiendpoint"> <br> API VERSION:<br> <input type="text" id="apiversion" name="apiversion"> ACCESSIBLE: <br> <input type="checkbox" name="source" value="internet"> Internet<br> <input type="checkbox" name="source" value="vpn"> VPN<br> <!-- <br><br> <input type="submit" formaction="Home" method="post" value="Submit"> --> <br> <input type="submit" id="check" name="check" value="Check"> </form> 

I have written an extra part of code in Example code Explanation: 我已经在示例代码中写了一部分代码说明:

  • Used a sample data 使用了样本数据
  • Loop through the data & updated the current links (instead of appending) 遍历数据并更新当前链接(而不是附加)
  • Example code is executing onLoad 示例代码正在执行onLoad

Like this: 像这样:

data.forEach(function(i){
   $('.vertical-menu a').attr('href',i);
}

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

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