[英]JS/Ajax: Submit form without page refresh or button click
I am trying to submit a form without a button click or page refresh. 我正在尝试提交没有单击按钮或刷新页面的表单。 Once the form is submitted then I will echo the value in the input field through php.
提交表单后,我将通过php在输入字段中回显值。 The problem is that I added a timer but is not doing anything at all.
问题是我添加了一个计时器,但什么也不做。 How can i set a timer once user stops typing give two seconds(keyup) and then take the value?
一旦用户停止键入,我该如何设置计时器,给两秒钟(keyup)然后取值? EXAMPLE
例
JS JS
<script>
$(function() {
var timer;
$(".submit").click(function() {
$('submit').on('keyup', function() {
var name = $("#name").val();
var dataString = 'name='+ name;
$.ajax({
type: "POST",
url: "index.php",
data: dataString,
success: function(result){
/*$('.success').fadeIn(200).show();
$('.error').fadeOut(200).hide();*/
$('#special').append('<p>' + $('#resultval', result).html() + '</p>');
}
});
return false;
});
}, 2000;
});
</script>
PHP 的PHP
<?php
if($_POST){
$url = $_POST['name'];
echo ('<b><span id="resultval">'.$url.'</span></b>');
}
?>
http://jsfiddle.net/earlonrails/pXA6U/2/ http://jsfiddle.net/earlonrails/pXA6U/2/
$(function() {
var timer = null;
var dataString;
function submitForm(){
alert("success");
$.ajax({ type: "POST",
url: "index.php",
data: dataString,
success: function(result){
alert("success");
}
});
return false;
}
$('#submit').on('keyup', function() {
clearTimeout(timer);
timer = setTimeout(submitForm, 2000);
var name = $("#name").val();
dataString = 'name='+ name;
});
});
I use the following to provide an auto-refreshing page with a visual timer. 我使用以下内容提供带有视觉计时器的自动刷新页面。 It does more than you need, but it could be stripped back to something simpler.
它的功能超出了您的需要,但可以剥离为更简单的方法。
Launch it at page load with 在页面加载时启动
auto_refresh();
Supporting functions below 以下支持功能
/**
* This function checks if the auto-refresh check box is checked and then refreshes the page.
*
*
*/
function auto_refresh() {
// ****************************************
// Countdown display
// ****************************************
$("#countdown").progressbar({ value: 100 });
check_refresh(120, 120);
$("#autorefresh").click(function() {
if ($(this).attr("checked") == "checked") {
$("#countdown").progressbar("option", "disabled", false );
$("#countdown").progressbar("option", "value", 100);
check_refresh(120, 120);
}
});
}
And... 和...
/**
* This functions sets the time interval used to auto-refresh the page.
*/
function check_refresh(countdownValue, secondsRemaining) {
var autorefresh = $("#autorefresh");
if ($(autorefresh).attr("checked") == "checked") {
setTimeout(function() {
var value = Math.round(secondsRemaining / countdownValue * 100);
// consoleDebug("Seconds remaining: " + secondsRemaining);
secondsRemaining -= 10;
$("#countdown").progressbar("option", "value", value);
if (secondsRemaining < 0) {
loadDashboard(); // <--- Launch whatever you want here.
check_refresh(120, 120);
} else {
check_refresh(countdownValue, secondsRemaining);
}
}, 10000);
} else {
$("#countdown").progressbar({ disabled: true });
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.