通过 javascript window.open 传递查询字符串值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8893341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 04:54:09  来源:igfitidea点击:

Passing a querystring value through a javascript window.open

javascriptasp.net

提问by Joshua

I need to pass the pass value stored in a asp hiddenfield to a querystring using the window.open.

我需要使用 window.open 将存储在 asp 隐藏字段中的传递值传递给查询字符串。

onclick="window.open('../New/FeedbackV4.aspx','FeedbackWindow','width=960,height=640,scrollbars=yes,resizable=yes,status=yes')"

I need to get the value of the hidden field and pass it as a querystring

我需要获取隐藏字段的值并将其作为查询字符串传递

采纳答案by Brissles

I'm not sure I quite understand the context, but given a HiddenField:

我不确定我是否完全理解上下文,但给出了一个HiddenField

<asp:HiddenField ID="hf_myhiddenfield" runat="server" Value="hidden value"/>

You can use a Javascript function to insert the value into your onclickattribute

您可以使用 Javascript 函数将值插入到您的onclick属性中

onclick

点击

onclick="window.open('../New/FeedbackV4.aspx'+GetHFValue(),'FeedbackWindow','width=960,height=640,scrollbars=yes,resizable=yes,status=yes')"   

Javascript

Javascript

<script type="text/javascript">
    function GetHFValue() {
        var hf_value = '?' + document.getElementById("<%= hf_myhiddenfield.ClientID %>").value;
        return hf_value;
    }
</script>

回答by Jukka K. Korpela

Assuming for simplicity that the hidden field has an idattribute, say id='foo'(if not, you will need to find another way of picking up that element), use

为简单起见,假设隐藏字段具有id属性,例如id='foo'(如果没有,您将需要找到另一种获取该元素的方法),使用

onclick="window.open('../New/FeedbackV4.aspx?' + par('foo'), ..."

(note the added “?”)

(注意添加的“?”)

with

<script>
function par(elid) {
  var elem = document.getElementById(elid);
  return encodeURI(elem.name) + '=' + encodeURI(elem.value);
}
</script>