我有一个带onclick功能的按钮。它将变量$ x传递给java脚本。我不想做的就是将$ x =“aaa”传递给javascript,并使函数将此变量更改为允许说$ x =“bbb”而不刷新网站。所以我可以一直点击它,js可以改变给定变量的值。我的意思是从按钮到javascript的内容变量将根据js函数代码进行更改。 这可能听起来很复杂,我真的很抱歉。有任何想法吗? (下面的代码我刚刚编写,因为这将很长时间来解释我现在很长的所有代码)
<?php x = "aaa"; ?>
<button onClick="someFunction(<?php $x ?>)"></button>
<script>
function someFunction (x){
//what can i do here to make this "aaa" string in variable change
//into for example "bbb" on clicking it again?
}
</script>
答案 0 :(得分:1)
您无法从客户端脚本
更改服务器端语言值您所能做的就是将该php值传递给javascript
喜欢这个
<button onClick="someFunction(<?php echo $x; ?>)"></button>
但你可以这样做
试试这个
js
$.ajax({
method: "GET",
url: "some.php",
data: { x: "John" }
})
.done(function( msg ) {
<?php x=$_COOKIE["X"]; ?>
});
<强> some.php 强>
$_COOKIE["X"]=$_GET["x"];
修改强>
How I can cut it off from button arguments
HTML
<button id="myButton" onclick="someFunction(<?php $x ?>)"></button>
JS
$("#myButton").on("click",function(){
var onclick=$(this).attr("onclick");
var str="Hellow world";
var someFunction="someFunction('"+str+"')";
$(this).attr("onclick",someFunction);
});
答案 1 :(得分:0)
考虑到上面的代码...不知道它是否是你想用javascript操作的特定字符串,但你可以使用php /服务器端语言根据你需要的任何计算生成变量值然后使用javascript在客户端使用它。
$service
答案 2 :(得分:0)
经过研究和其他人的帮助,这是有效的:
//This gets the page's width on the first load
let sWidth = window.screen.width;
//This says : execute "myfunction" when "resize" event occurs
window.addEventListener("resize", myFunction);
//details of myFunction
function myFunction() {
//first get the page's width again
let sWidth = window.screen.width;
//show me the page's width within console which is within inspect
console.log(sWidth);
//now this is for differentiating Mobile and Desktop version
//which you can change the statement by need
if (sWidth <= 576){
console.log("Mobile version");
}else{
console.log("Desktop version");
}
}