【问题标题】:How to pass javascript textBox value to URL as parameter如何将 javascript textBox 值作为参数传递给 URL
【发布时间】:2012-11-09 18:44:14
【问题描述】:

我想将日期和时间选择器中的值作为参数传递给 URL。这就是我所拥有的:

<div style="text-align:left; margin:8px 5px auto;">
        <label for="dt_id">Please enter a date and time </label>
        <input type="Text" id="dt_id" maxlength="25" size="25"/>
        <img src="../../js/datetimepicker/images2/cal.gif" onclick="javascript:NewCssCal('dt_id','yyyyMMdd','arrow','true','24')" style="cursor:pointer"/>
        <input type='button' onclick='setURL()' value='SUBMIT'> 
</div>

<script>
var dt_value = document.getElementById("dt_id").value;
var sjdurl =  "/path/script?datetime="+dt_value;
</script>

但是,URL 不包含日期值。

【问题讨论】:

  • 你的 JS 代码应该在 setURL() 函数中..
  • 我会先检查 alert(dt_value) 或 console.log(dt_value) 看看你是否在获取值,然后从那里开始。
  • 另外,URL 中是完全出现还是什么也没有出现?您可能需要使用 encodeURI 和 decodeURI [developer.mozilla.org/en-US/docs/JavaScript/Reference/…]

标签: javascript url parameters textbox


【解决方案1】:

我试图重现这个场景,它完全适合我:-

<html>
<head>
<script>
    function setURL(){
    var dt_value = document.getElementById("dt_id").value;
    //just test here ..what is coming..
    alert(dt_value );
    var sjdurl =  "www.google.com?datetime="+dt_value;

popup = window.open(sjdurl,"popup"," menubar =0,toolbar =0,location=0, height=900, width=1000"); 
popup.window.moveTo(950,150); 

    }
</script>
</head>
<body>
<div style="text-align:left; margin:8px 5px auto;">
        <label for="dt_id">Please enter a date and time </label>
        <input type="Text" id="dt_id" maxlength="25" size="25"/>
        <input type='button' onclick='setURL()' value='SUBMIT'> 
</div>

</body>
</html>

【讨论】:

  • 哎呀我忘了提:我在弹出功能中使用 url:popup = window.open(sjdurl,"popup","menubar =0,toolbar =0,location=0, height= 900, 宽度=1000");我也用了alert(dt_value)检查,值是正确的!
  • sjd.node.onclick = function () { popup = window.open(sjdurl,"popup"," menubar =0,toolbar =0,location=0, height=900, width=1000 "); popup.window.moveTo(950,150); if (window.focus){popup.focus}; };
  • 两件事:- 1> 你是如何设置 var sjdurl 的,在 setURL 函数内部或全局。如果在 SetURL 函数中,那么您将如何在 openpopup 函数中访问该变量,因为它将超出范围。2> 如果是全局变量,那么您将如何设置变量。?
  • 问题已解决。我的 onclick 函数在 setURL 函数之外。
  • 如果解决了很好..但是如果我的回答对您有帮助,那么您应该投票并接受答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多