簡體   English   中英

從Url獲​​取變量

[英]Getting variable from the Url

我有一個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

這可能無法完全回答您的問題,但請考慮以下事項:

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

傳遞參數時應該應用正確的轉義:

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

要從HTML頁面訪問txttread的值,請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

我認為這與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());">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM