2016-08-03 77 views
2

我想通過一個可能包含'+'符號的變量,但是在傳入下一頁後,它會變爲連接。 '2 + 2',從first.php到second.php在second.php上顯示爲'2 2',但我只需要'2 + 2',同樣對'2-2'也適用。這是我的first.php在JS變量中傳遞'+'符號

<script> 
$(document).ready(function() { 
    $("#sub").click(function() { 
var txt1 = $("#userquery").val(); //textbox value 
     $.ajax({ 
      type: "POST", 
      url: "second.php", 
      cache: false, 
      data: "txt1=" + txt1,  
      dataType: "html", 
      success: function(result) { 
       $("#sqlresult").html(result); 
      } 
     }); 
    }); 
}); 
</script>  
<textarea id='userquery' rows='5' cols='115' spellcheck='false' wrap='off' placeholder='Write SQL query here..'></textarea> 
<input id='sub' type='submit' value='Execute' title='Ctrl+Enter'> 
<hr><div id='sqlresult'><?php include('second.php'); ?></div> 

回答

2

你傳遞一個字符串data,這意味着你要爲此承擔責任而正確編碼(但不能正常之前只是URL編碼您的價值編碼它)。它發生在URI編碼中,+意味着空間。

只是把它作爲一個對象,jQuery將妥善處理編碼爲您:

data: {txt1: txt1} 
+0

非常棒!它的工作,答案接受。謝謝 –

-1

嘗試這樣

"text"+ '+' + "text2" 
1

你必須通過它像一個URL參數

var txt1 = encodeURIComponent($("#userquery").val()); 
+0

這也工作過。謝謝+1,upvote –