Javascript 如何通过jQuery/ajax将下拉样式值传递到php文件?
以下是我的下拉列表的标记:Javascript 如何通过jQuery/ajax将下拉样式值传递到php文件?,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,以下是我的下拉列表的标记: <select id="themes"> <option selected="selected">Themes:</option> <option value="default">Default</option> <option value="red">Red</option> <option value="
<select id="themes">
<option selected="selected">Themes:</option>
<option value="default">Default</option>
<option value="red">Red</option>
<option value="bw">Black and White</option>
<option value="invert">Invert</option>
</select>
这给了我一个控制台错误“uncaughtsyntaxerror:unexpectedtoken+”。我所要做的就是将$\u GET['theme']传递到我的style.php,这样我就可以用条件处理它了。有什么建议吗?将数据创建为对象,代码中存在语法问题
$("#themes").change(function () {
$.ajax({
url: "css/style.php",
method: "GET",
data: {
theme: $(this).val()
}
})
});
您的代码有问题。在JavaScript中创建对象时,需要提供键值对
像这样做
$("#themes").change(function(){
var theme = $(this).val();
$.ajax({
url: "css/style.php",
method: "GET",
data: {theme : theme },
success: function(data) {
}//end of success
});
});
我总是使用这种技术,然后使用
$theme = $_GET['theme'];
谢谢你的建议。但是,我仍然收到相同的JS控制台错误,我的!我的php文件中的空检查仍然显示未设置$\u GET['theme']。有什么想法吗?我已经创建了一个JSFIDLE。客户端没有问题。清除php缓存…真的很奇怪,但仍然不工作。这是css/style.php文件:请再次检查我在主题中错误地添加了s,请重新检查,这对我仍然不起作用。下面是我在style.php中的实现:当我在浏览器中导航到该页面时,该页面的背景仍然设置为黑色,当我尝试回显它时,$_GET['theme']出现未定义的索引错误。和我的代码,直接来自JS文件:
$("#themes").change(function(){
var theme = $(this).val();
$.ajax({
url: "css/style.php",
method: "GET",
data: {theme : theme },
success: function(data) {
}//end of success
});
});
$theme = $_GET['theme'];