search

Home  >  Q&A  >  body text

javascript - The ajax I wrote has no effect. Please guys, please find out what the problem is...

My purpose is that the value of the href attribute of the original a tag is false.
I want the user to change the value of the href attribute after clicking the a tag and lead to a link.
This link is http ://XXX.XXX.html/?key='name value in data'&tit='title value in data'.
The data passed from the data background,
contains an array [{"name":"haha","title":"123"},{"name":"haha","title": "4567"}], is such data,
Then when using ajax to transmit the data in the background, the corresponding values ​​​​are combined into the form of a link, and passed to the href value of the a tag,
so that the user can When clicked, jump to the page corresponding to each a tag.

PS: I am a newbie and I don’t know much about it. The code I wrote is very problematic, but I don’t know what to do. Please help me! Kneel down and thank you!

The following is the code part:

<p class="xixi">

<a href="haha/form.html">1</a>
<a href="haha/form.html">2</a>
<a href="haha/form.html">3</a>
<a href="haha/form.html">4</a>
<a href="haha/form.html">5</a>

</p>

<script>

$.ajax({
    type:"get",
    url:"aaa.json",
    success:function(data){
        $(".xixi a").on('click','a',function(data){
           for(var i = 0;i < data.length;i++){
               for(var j = 0;j < data.length[i];j++){
                   var zName = data[i][j].name;
                   var zTitle = data[i][j].title;
                   $(this).attr('href','haha/form.html?key="+zName+"&tit="+zTitle"');
               }
           }
        });
    }
})

</script>

In addition, I have another question for you guys. I can’t see ajax in the chrome browser.
But when I checked it in firefox, no error was reported. Can I use firefox to see if ajax has any effect?

迷茫迷茫2745 days ago553

reply all(6)I'll reply

  • 仅有的幸福

    仅有的幸福2017-05-27 17:44:32

    $.ajax({
        type: 'get',
        url: 'aaa.json',
        success: function(data){
            // 遍历数据或者遍历 jQuery 对象
            $('.xixi > a').each(function(i,link){
                // 不明白你为什么要使用两次 for 循环查找数据;
                // 你的数组是一个二维数组,可以使用 data[0].name 这种格式访问;
                var name = data[i].name,
                    title = data[i].title;
                    
                // 绑定事件
                $(link).off()
                .on('click', function(e){
                    // 我建议你尽量使用单引号(除非有特殊规定),这是个好习惯!
                    $(this).attr('href','haha/form.html?key='+ name +'&title='+ title);
                });
                
            });
        }
    })

    reply
    0
  • phpcn_u1582

    phpcn_u15822017-05-27 17:44:32

    $(this).attr("href","haha/form.html?key="+zName+"&tit="+zTitle);

    I feel like I wrote the wrong address. Please see if this works.

    reply
    0
  • 迷茫

    迷茫2017-05-27 17:44:32

    First of all, what kind of array is this [{"name":"haha","title":"123"},{"name":"Haha","title"}:"4567"], what the hell is this part? "title"}:"4567"

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-27 17:44:32

    I really don’t quite understand what the poster meant~ I’ll just make a rough guess: the link to the a tag is obtained through ajax, and it’s not there from the beginning~ In addition, see $符号,我就猜楼主用的jquery. The specific implementation of my code below should be achievable~

    Execute directly after the page is loadedajax,获得链接后修改所有a标签的链接。(楼主处理ajaxThe result is this paragraph, the for loop, I don’t understand it very well~)

    <!doctype html>
    <html>
    <meta charset="utf-8">
    <script src="./jquery-3.2.1.min.js"></script>
    
    <body>
      <p class="xixi">
    
        <a href="#">1</a>
        <a href="#">2</a>
        <a href="#">3</a>
        <a href="#">4</a>
        <a href="#">5</a>
      </p>
    </body>
    
    <script>
      $(document).ready(function() {
        /*页面载入时候执行ajax*/
        $.ajax({
          type: "get",
          url: "aaa.json",
          success: function(data) {
            /*ajax成功或,改变所有的a标签的href*/
            for (var i = 0; i < data.length; i++) {
              for (var j = 0; j < data.length[i]; j++) {
                var zName = data[i][j].name;
                var zTitle = data[i][j].title;
                $(". xixi a").eq((i + 1) * (j + 1)).attr('href', 'haha/form.html?key="+zName+"&tit="+zTitle"');
              }
            }
          }
        })
      });
    
    </script>
    
    </html>

    Also, where can I watch ajax in chrome? F12打开开发者模式,然后选network

    reply
    0
  • 阿神

    阿神2017-05-27 17:44:32

    The problem of string splicing behind href is not ajax problem

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-05-27 17:44:32

    [{"name":"Hehe","title":"123"},{"name":"Haha","title":"4567"}]
    This is a one-dimensional array, traverse the objects in it , only one loop is needed to organize name and title

    reply
    0
  • Cancelreply