我正在尝试传递一个可能包含“+”符号的变量,但在传递下一页后它会被连接起来。
假设我传递 '2+2' ,从 first.php 到 second.php 上的 second.php 它显示为 '2 2'。但我只需要它作为 '2+2'。这同样适用于“2-2”。这是我的first.php
<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>
最佳答案
您正在为 data
传递一个字符串,这意味着您负责对其进行正确编码(但未正确编码)。碰巧在URI编码中,+
表示空格。
只需将它作为一个对象传递,jQuery 就会为您处理正确的编码:
data: {txt1: txt1}
关于javascript - 在 JS 变量中传递 '+' 符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38742126/