简体   繁体   English

更改ajax return内的anchor的href,但随后被覆盖

[英]changing the href of anchor inside ajax return but then it is overwritten

Hello all i have a problem i have a table with a link in one of the columns on the click of that link i send it to a function that uses ajax to get a different link for it to direct to. 大家好,我有一个问题,我在该链接的点击后的某列中有一个链接表,我将其发送给使用ajax的函数,以获取指向该链接的其他链接。 inside the ajax function it is being changed correctly but then after it finishes the ajax it still redirects to the original page. 在ajax函数中,它已正确更改,但是在完成ajax之后,它仍然重定向到原始页面。 if i change it outside of the ajax return it works correctly and redirects to a different site 如果我在ajax之外将其更改,则可以正常工作并重定向到其他站点

this is my code: 这是我的代码:

the php function for building the table 用于建立表格的php函数

function populate()
{
$link = connectToDB();
$query = 'Select value1, value2
         from table1';
$data = read($query, $link);
$table = '<div id="table"><table id="links">
            <tr>
            <th>value1</th>
            <th>value2</th>   
          </tr>';

$num =0;
while ($row = $data->fetch_assoc()) {
    $table .= '<tr>';
    $table .= '<td>' . $row['value1'] . '</td>';
    $table .= '<td><a id="redirection'.$num.'" onclick="redirect('.$num.')" href=”/?redirection.php?' . $row['value2'] . '>' . $row['value1'] .'</td>';
    $table .= '</tr>';
    $num++;
}
$table .= '</table></div>';
mysqli_close($link);
return $table;
}

the javascript to change the href 更改href的javascript

function redirect(num){
    var url = $("#redirection"+num).attr("href");
    var sub = url.split('?');
    var val1= sub[2];
    var test = "";
    $.ajax({
        url: 'getNewUrl.php',
        type:'POST',
        dataType: 'json',
        data: { name: val1},
        success: function(table){
            alert(table.val2);
            $("#redirection"+num).attr("href",table.val2);
            var check =$("#redirection"+num);
        },
        error: function () {
            alert("No value with that name");
        }
    });
}

when i am alerting the value retured it is the correct one, and when i am checking the href value of check in debugger it is being changed correctly but then i am still redirected to the wrong site 当我提醒重置的值是正确的值时,并且当我检查调试器中的check的href值时,它已正确更改,但随后我仍然重定向到错误的站点

thanks for the help 谢谢您的帮助

i think the problem is in href=”/? 我认为问题出在href=”/? and wrong doublequote and miss close quote and remove "?" 和错误的双引号,并且错过了右引号并删除了"?" after =/ =/

onclick="redirect('.$num.')" href=”/?redirection.php?' . $row['value2'] . '

it's sould be like this. 就像这样

onclick="redirect('.$num.')" href="/redirection.php?' . $row['value2'] .'"

hope this help. 希望有帮助。

Okay so i as going about this all wrong and just figured it out instead of changing the href and then having it redirect me to a page i just used window.location to redirect me from inside the ajax and the href change was redundent 好的,这样我就解决了所有错误,只是想出了办法而不是更改href,然后让它将我重定向到页面,我只是使用window.location从ajax内部重定向了我,并且href更改是多余的

Thanks to all who tried to help me 感谢所有试图帮助我的人

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

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