Php 当am使用e.preventDefault()时,查询字符串未追加

Php 当am使用e.preventDefault()时,查询字符串未追加,php,jquery,Php,Jquery,//我使用下面的Jquery向下滑动Div,它与e和// //e、 防止违约。如果我使用它,它只是向下滑动,而不显示 //content i//表示查询字符串 //Jquery // I used the below Jquery for slide down the Div its working fine with e and // //e.preventDefault(). If am using that it just slidedowns and not displaying

//我使用下面的Jquery向下滑动Div,它与e和// //e、 防止违约。如果我使用它,它只是向下滑动,而不显示 //content i//表示查询字符串

//Jquery 

// I used the below Jquery for slide down the Div its working fine with e and   //
//e.preventDefault(). If am using that it just slidedowns and not displaying the 
//content i //mean the query string


$(".edit_inven").click(function (e) {
    $("#edititems").hide().slideDown('slow');
e.preventDefault()
});


//hyperlinks for same page

e、 preventDefault阻止a标记的默认行为

<a href='inventory_list.php?pedit_id=1' class='edit_inven'>Edit</a>
<a href='inventory_list.php?pedit_id=2' class='edit_inven'>Edit</a>
<a href='inventory_list.php?pedit_id=3' class='edit_inven'>Edit</a>
<a href='inventory_list.php?pedit_id=4' class='edit_inven'>Edit</a>


//Div which holds php statements that echo outs some values   

<DIV id='edit_inven' style="display:none">
echo $_GET['pedit'];
</DIV>

尝试用php标记包装echo

<!-- Default behavior upon clicking is to go to index.php. If you prevent the default, clicking does nothing unless otherwise specified -->
<a href='/index.php'>Link!</a>
试一试

在你的代码中

window.history.pushState("", "", '/newpage');

锚定链接的致命行为是跳转到另一个文档。另外,当请求到达服务器时,应该执行PHP代码$\u GET['pedit\u id']。在这里,您将阻止锚链接的默认行为,因此不会向服务器发送任何请求。不过,如果需要访问查询字符串值,可以使用jQuery本身进行访问

$(".edit_inven").click(function (e) {
    $("#edititems").hide().slideDown('slow');
    window.history.pushState("", "", $(this).attr('href')); // here
    e.preventDefault()
});
为用户更新的部件:

如果希望基于url参数加载产品,则不需要进行上述解析。你可以试试看

$(".edit_inven").click(function (e) {

    e.preventDefault();
    var QS = $(this).attr('href').split('?');
    if(QS.length>0){
        var val = QS[1].split('=');

        if(val.length>0){
          $("#edititems").html(val[1]).hide().slideDown('slow');
        }    
    }
});

请参阅工作演示

为什么会这样,这就是阻止锚的默认操作应该做的。您已经阻止了它的默认行为。!阻止默认操作是为了防止anhor重新加载页面和更改URL,所以这是您所期望的。如果需要从服务器加载数据而不重新加载,你必须使用ajax。我试图完全这样做。attr'href'包含它显示url和附加到它的查询字符串,但当我试图从php访问查询字符串时,它无法获取查询字符串。我保留了你建议我的代码,但它与查询字符串一起显示在url中,但当我尝试访问查询字符串它没有在我使用的同一页中将dat作为查询字符串处理ifisset$_GET['pedit_id']{}else{}它执行else块$.edit_inven.clickfunction e{e.preventDefault;var QS=$this.attr'href'.split'?';ifQS.length>0{var val=QS[1]。split'=';ifval.length>0{document.getElementByIdval.value=val[1];$edititems.hide.slideDown'slow';};$prdid=;$qry=mysql\u queryselect*from products,其中id=$prdid;以上两个PHP语句是否有效?如果不是,我该怎么做?您想加载产品,对吗?为此,您可以发送一个AJAX请求来检索产品详细信息。我在同一页面中使用了dat下的div标记。我有一个显示为:none i的表单最初,当我单击edit时,它应该会显示。我现在会向您发送更多详细信息//Div,其中保存了一些值的php语句//这里我有一个与产品$.edit_inven.clickfunction e{e.preventDefault;var QS=$this.attr'href'.split'?;alertQS.length ifQS.length>0相关的编辑表单{var val=QS[1].split'=';alertval ifval.length>0{$edititems.htmlval[1].hide.slideDown'slow';}}};你建议我使用这个,我怎么可能也得到jquery的效果呢
$(".edit_inven").click(function (e) {

    e.preventDefault();
    var QS = $(this).attr('href').split('?');
    if(QS.length>0){
        var val = QS[1].split('=');

        if(val.length>0){
          $("#edititems").html(val[1]).hide().slideDown('slow');
        }    
    }
});
$(".edit_inven").click(function (e) {
     e.preventDefault();
     var url =  $(this).attr('href');
        $.ajax( url )
        .done(function(response) {
                 $("#edititems").html(response).hide().slideDown('slow');
        })
        .fail(function() {
              alert( "error" );
       });
});