繁体   English   中英

如何通过javascript传递网址中的多个参数

[英]how to pass multiple params in url through javascript

我在javascript中有if-else条件。 在其他所有条件中,我都想在&的基础上在url中传递参数id及其值,就像在其他条件条件代码中传递带有id变量的value 5一样。

http://localhost/cah/blank.php?id=5

在第二状态

http://localhost/cah/blank.php?id=5&id=9

等等。 同样,我可以有多个条件,其中必须在url中传递不同的id值。 如何做呢? 通过将它们全部存储在数组中,或者基于&传递url中的值,但不刷新页面。

var count=0;
var counts=0;

function check() {
    var a = document.getElementById("div1") ;
    var b = document.getElementById('div1').getAttribute('value');

    if (a.textContent==b){
        count++;
        var p1=document.getElementById('cnt');
        p1.value=count;
        a.style.color = "green";
    }
    else { 
        counts++;
        a.style.color = "Red";
        window.history.pushState("object or string", "Title", "./blank.php?id=<? echo $id;?>");
        $('#d<? echo $d3++;?>').addClass('disabled');
    }
}

使用数组样式名称作为参数:

http://localhost/cah/blank.php?id[]=5&id[]=9

然后,PHP将使$_GET['id']返回所有参数的数组。

 var ids = [5, 9]; var id_params = ids.map(function(id) { return 'id[]=' + id; }).join('&'); var url = 'http://localhost/cah/blank.php?' + id_params; console.log(url); 

在PHP中将是:

<?php
$ids = array(5, 9);
$id_params = implode('&', array_map(function($id) {
    return 'id[]=' . $id;
}, $ids));
?>
window.history.pushState("object or string", "Title", "./blank.php?<?php echo $id_params;?>");

暂无
暂无

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

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