简体   繁体   English

如何使用 onclick 将隐藏的 ID 值传递到另一个页面

[英]How to pass hidden ID value to another page using onclick

I would like to know how do I pass the hidden value to another page using onclick here is my code:我想知道如何使用 onclick 将隐藏值传递到另一个页面,这是我的代码:

<button type="button" id="requestthis" class="btn btn-primary" onclick="pass()"><a href="submitrequest.php">Request</a></button>
<input type="hidden" id="hidden_user_id">

I used this function but it didn't seem to work.我使用了这个功能,但它似乎没有用。

<script type="text/javascript">
    function pass(id)
    {
        url: 'submitrequest.php'+id;
    }
</script>

Other sites I visited didn't do any help.我访问过的其他网站没有任何帮助。

Anyone who could point out where did I do wrong?谁能指出我哪里做错了? Thanks in advance.提前致谢。

You don't need to pass anything to pass你不需要通过任何东西来pass

function pass()
    {
        var hiddenVal = $("#hidden_user_id").val();
        url= 'submitrequest.php?hidden_vale='+hiddenVal;
    }

It's better to avoid inline-event onClick() and attach event in the JS code, check example bellow.最好避免内联事件onClick()并在 JS 代码中附加事件,请查看下面的示例。

HTML : HTML :

<button type="button" id="requestthis" class="btn btn-primary">
    <a href="submitrequest.php">Request</a>
</button>

<input type="hidden" id="hidden_user_id">

JS : JS:

<script type="text/javascript">
    $(function() { //ready function
        $('#requestthis').on('click', function(e){ //click event
            e.preventDefault(); //prevent the click from redirecting you to "submitrequest.php"

            var hidden_id = $('#hidden_user_id').val();
            var url = "submitrequest.php?hidden_id="+hidden_id;

            window.location.replace(url);
            //OR 
            //window.location.href = url;
        })
    })
</script>

To get the parameter id in the other page you have to use $_GET[] , example :要在其他页面中获取参数id ,您必须使用$_GET[] ,例如:

echo $_GET['hidden_id'];

Hope this helps.希望这可以帮助。

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

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