从Url获​​取变量

时间:2012-06-02 14:56:36

标签: php javascript jquery

我有一个jquery函数,用于检索用户在数据库表中单击的信息。用户可以选择在鼠标悬停时突出显示的十行中的任何一行,当用户单击突出显示的行时,函数会检索并放置它进入文本框。然后,如果用户提交此购买请求,我想回显下一页上的文本框,这是一个订单。

以下代码效果很好,直到我尝试从网址中检索信息。我可以看到它在url中传递到下一页但是在尝试了两天后我无法检索它。我不知道从哪里开始。有人可以看看这个,看看我没有正确编码或做错了什么。 我已经复制了适用的代码......

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">


$(document).ready(function(){
  $("table tr").click(function(){
       $("#txttread").val($.map($(this).children('td:not(:eq(7))'), function (item) { return $(item).text() }).join(' - '));
  });
});


$(document).ready(function() {

    $('.pickme tr').not(':first').hover(
        function() { $(this).addClass('highlight'); },
        function() { $(this).removeClass('highlight'); }
    ).click( function() {
        $('.selected').removeClass('selected');
        $(this).addClass('selected').find('input').attr('checked','checked');
    });
});


</script>
</head>
<body>
<form action="Order.html" method="GET" name="myform2" />



<div>
<div style="text-align:left height:250px;">
<DIV STYLE="font-family: Arial Black;
color: black; font-size: 20pt;">

Select from inventory below:<br/><input type="text" style="width:500px; height:35px;" rows="1" STYLE="font-family: Arial Black;
color: red; font-size: 20pt;" name="txttread" id="txttread" DISABLED /></div></div></div>
<div>
<div style="text-align:center;">



<br/><input type="button" button id="getone" name="getone" value="Submit your request for purchase" onclick="window.location.href = 'http://localhost/order.html?txttread='+ ( $('#txttread').val() )"><br/><hr/>
</body>
</html>

下一页的网址是....

    http://localhost/order.html?txttread=Firestone - All Season - FR-710 - 225/60/16 - 4 - 3 - 60.00

2 个答案:

答案 0 :(得分:1)

这可能不会完全回答你的问题,但请考虑一下:

window.location.href = 'http://localhost/order.html?txttread='+ ( $('#txttread').val() )

传递参数时应该应用正确的转义:

window.location.href = 'http://localhost/order.html?txttread=' + encodeURIComponent( $('#txttread').val() );

要从HTML页面访问txttread的值:

function getParameterByName(name)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(window.location.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

在此处找到:https://stackoverflow.com/a/901144/1338292

答案 1 :(得分:1)

我认为这与未正确编码的URL有关。在您附加$('#txttread').val()的最后一行,您应该使用encodeURIComponent()包装它:

<input type="button" 
       button id="getone" 
       name="getone" 
       value="Submit your request for purchase" 
       onclick="window.location.href = 'http://localhost/order.html?txttread=' + encodeURIComponent($('#txttread').val());">