R 将数据发送到服务器并返回会延迟
我希望通过按下方形div/按钮(页面上的第三个元素),元素下会出现一个随机数,元素的颜色也会改变。不幸的是,我似乎必须按下页面顶部的按钮才能发生这种情况,我不知道为什么 myindex.htmlR 将数据发送到服务器并返回会延迟,r,shiny,R,Shiny,我希望通过按下方形div/按钮(页面上的第三个元素),元素下会出现一个随机数,元素的颜色也会改变。不幸的是,我似乎必须按下页面顶部的按钮才能发生这种情况,我不知道为什么 myindex.html <html> <head> <script type="text/javascript" src="shared/jquery.js"></script> <script type="text/javascri
<html>
<head>
<script type="text/javascript" src="shared/jquery.js"></script>
<script type="text/javascript" src="shared/shiny.js"></script>
</head>
<body>
<input id="button" type="submit" name="submit" value="Press me!" class="action-button">
<p><div id="text" class="shiny-text-output" style="width: 150px; height: 25px; border: 1px solid black;"></div>
<p><div id="mydiv" style="width: 50px; height: 50px;"></div>
<pre id="results" class="shiny-text-output"></pre>
</body>
<script type="text/javascript">
// send data to shiny server
document.getElementById ("mydiv").onclick = function() {
var number = Math.random();
Shiny.onInputChange ("mydata", number);
};
// handlers to receive data from server
Shiny.addCustomMessageHandler ("myColorHandler",
function (color) {
document.getElementById ("mydiv").style.backgroundColor = color;
}
);
</script>
</html>
将按钮上的type=“submit”
更改为type=“button”
。当前,您的按钮充当的是提交按钮
,而不是操作按钮
<html>
<head>
<script type="text/javascript" src="shared/jquery.js"></script>
<script type="text/javascript" src="shared/shiny.js"></script>
</head>
<body>
<input id="button" type="button" name="submit" value="Press me!" class="action-button">
<p><div id="text" class="shiny-text-output" style="width: 150px; height: 25px; border: 1px solid black;"></div>
<p><div id="mydiv" style="width: 50px; height: 50px;"></div>
<pre id="results" class="shiny-text-output"></pre>
</body>
<script type="text/javascript">
// send data to shiny server
document.getElementById ("mydiv").onclick = function() {
var number = Math.random();
Shiny.onInputChange ("mydata", number);
};
// handlers to receive data from server
Shiny.addCustomMessageHandler ("myColorHandler",
function (color) {
document.getElementById ("mydiv").style.backgroundColor = color;
}
);
</script>
</html>
//将数据发送到服务器
document.getElementById(“mydiv”).onclick=function(){
var number=Math.random();
.onInputChange(“mydata”,编号);
};
//从服务器接收数据的处理程序
shinny.addCustomMessageHandler(“myColorHandler”,
功能(颜色){
document.getElementById(“mydiv”).style.backgroundColor=颜色;
}
);
<html>
<head>
<script type="text/javascript" src="shared/jquery.js"></script>
<script type="text/javascript" src="shared/shiny.js"></script>
</head>
<body>
<input id="button" type="button" name="submit" value="Press me!" class="action-button">
<p><div id="text" class="shiny-text-output" style="width: 150px; height: 25px; border: 1px solid black;"></div>
<p><div id="mydiv" style="width: 50px; height: 50px;"></div>
<pre id="results" class="shiny-text-output"></pre>
</body>
<script type="text/javascript">
// send data to shiny server
document.getElementById ("mydiv").onclick = function() {
var number = Math.random();
Shiny.onInputChange ("mydata", number);
};
// handlers to receive data from server
Shiny.addCustomMessageHandler ("myColorHandler",
function (color) {
document.getElementById ("mydiv").style.backgroundColor = color;
}
);
</script>
</html>