" /> -6ren">
gpt4 book ai didi

jquery - 单击按钮时的 Ajax 加载程序 - 代码错误

转载 作者:行者123 更新时间:2023-12-01 05:18:01 24 4
gpt4 key购买 nike

我遵循了有关在单击按钮提交表单时显示 Ajax 加载 Gif 的其他建议,但我似乎无法使代码正常工作。

非常感谢任何帮助。

    <p>
<input type="hidden" name="job_manager_form" value="<?php echo $form; ?>" />
<input type="hidden" name="job_id" value="<?php echo esc_attr( $job_id ); ?>" />
<input type="hidden" name="step" value="<?php echo esc_attr( $step ); ?>" />

<?php if($job_id){ ?>
<img src="https://www.salusa.co.uk/wp-content/uploads/2017/12/ajax-loader.gif" id="img" style="display:none"/ >
<input type="submit" id="submitjob" name="submit_job" class="button" value="Update my service profile" />


<?php } else { ?>
<img src="https://www.salusa.co.uk/wp-content/uploads/2017/12/ajax-loader.gif" id="img" style="display:none"/ >
<input type="submit" id="submitjob" name="submit_job" class="button" value="Submit profile for admin approval" />
<?php } ?>

</p>

<script>
$('#submitjob').click(function(){
$('#img').show();
$.ajax({
....
success:function(result){
$('#img').hide(); //<--- hide again
}
}
</script>

最佳答案

如果您的 Ajax 请求出现错误或者您没有包含必要的 jquery 脚本,则该代码将无法工作。根据您上面所做的,我有一个工作示例,供您引用:

<html>

<head>
<title>Ajax Example</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
</head>

<body>
<input type="hidden" name="job_manager_form" value="<?php echo $form; ?>" />
<input type="hidden" name="job_id" value="<?php echo esc_attr( $job_id ); ?>" />
<input type="hidden" name="step" value="<?php echo esc_attr( $step ); ?>" />


<!-- Here comes your if condition -->
<img src="https://www.salusa.co.uk/wp-content/uploads/2017/12/ajax-loader.gif" id="img" style="display:none" />
<input type="submit" id="submitjob" name="submit_job" class="button" value="Update my service profile" />

<div id="content"></div>
</body>

<script>
$('#submitjob').click(function() {
$('#img').show();
$.ajax({
url: 'https://jsonplaceholder.typicode.com/posts/1',
type: 'GET',

error: function() {
$('#content').html('<p>An error has occurred</p>');
},
dataType: 'json',
success: function(result) {
$('#content').html("data Received" + result);
$('#img').hide(); //<--- hide again
},
});
});
</script>
</body>

</html>

关于jquery - 单击按钮时的 Ajax 加载程序 - 代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47696421/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com