gpt4 book ai didi

javascript - 如果文本框不为空,则拒绝页面刷新

转载 作者:行者123 更新时间:2023-11-30 14:12:55 25 4
gpt4 key购买 nike

如果文本框不为空则勾选,否则刷新页面。

我尝试检查 textbox 的 id 是否为 == null 然后页面将每 5 秒刷新一次,如果它的 != null 或不为空 页面不刷新。

根据20yco的要求是完整代码:

{% block body %}
<head>

<link rel="stylesheet" type="text/css" href="{% static '/css/simpleInformation.css' %}">
<link rel="stylesheet" type="text/css" href="{% static '/css/simpleInformationSearch.css' %}">

</head>

<body>
{# refresh the page each 50 second #}
<script type="text/javascript">

// if($('#opinion').val() == null ){
// window.setInterval(function () {
// location.href = " ";
// }, 5000);
// }

var myTimer = setInterval('window.location.reload()', 5000);

$('#opinion').on('keyup change', function(){
if( $(this).val().length == '' ) {
myTimer = setInterval('window.location.reload()', 5000);
} else {
clearInterval(myTimer);
}
});


</script>

<form class="form-style-9">
<!-- {{ Queryresult }} -->

<table border="1" style=" border-style:solid; width: 100%; ">
<thead>
<tr style=" text-align: center; padding: 8px; background-color: #4CAF50;
color: white;">
<th style=" text-align: center; padding: 8px; background-color: #2D77A2;
color: white;">
add opinion
</th>
<th style=" text-align: center; padding: 8px; background-color: #2D77A2;
color: white;" >
num
</th>
<th style=" text-align: center; padding: 8px; background-color: #2D77A2;
color: white;">
title
</th>
<th style=" text-align: center; padding: 8px; background-color: #2D77A2;
color: white;">
date
</th>
<th style=" text-align: center; padding: 8px; background-color: #2D77A2;
color: white;">
description
</th>
</tr>
</thead>
{% for item in Queryresult %}

<tr style=" text-align: center; padding: 8px;">
<td style=" text-align: center;" >
<a href="#" class="te2">
{{ item.te2chira_id }}
</a>
</td>
<td style=" text-align: center;">
{{ item.num }}
</td>
<td style=" text-align: center;">
{{ item.title }}
</td>
<td style=" text-align: center;">
{{ item.te2chira_date }}
</td>
<td style=" text-align: center;">
{{ item.description }}
</td>
</tr>

{% endfor %}
</table>
<br>
<br>
<ul>
<li>
<input type="hidden" id="theid">

<input type="date" name="field2" class="field-style field-split align-left" placeholder="date" id="date" readonly/>

</li>

<li>
<input type="number" name="field3" class="field-style field-split align-right" placeholder="num" id="num" readonly/>
</li>
<li>
<input type="text" name="field4" class="field-style field-full align-none" placeholder="title" id="title" readonly/>
</li>
<li>
<textarea name="field5" class="field-style" placeholder="description" id="text" readonly></textarea>
</li>
<li>
<table id="opinions_table" class="searchte2chira">

</table>
</li>
<br>

<li>
<textarea name="field5" class="field-style" placeholder="opinion" id="opinion"></textarea>
</li>

<li>
<input type="submit" class="field-style field-full align-none" value="Add opinion " id="updateform" />
</li>
</form>


<script type="text/javascript">
$(function(){
var theSections={}
$('.te2').on('click',function(e){
e.preventDefault()
$.ajax({
'method':'POST',
'url':'/getTe2chira',
'data':{
id:$(e.target).text()
},
headers: {
'X-CSRFToken': '{{csrf_token}}'
}
}).done(function (msg) {
console.log(msg)

$('#year').val(msg['te2']['year'])
$('#date').val(msg['te2']['te2chira_date'])
$('#num').val(msg['te2']['num'])
$('#title').val(msg['te2']['title'])
$('#text').val(msg['te2']['description'])
$('#theid').val(msg['te2']['te2chira_id'])
$.ajax({
'method':'POST',
'url':'/getOpinion',
'data':{
'id':$('#theid').val()
},
headers: {
'X-CSRFToken': '{{csrf_token}}'
}
}).done(function(resps){
//console.log(resps)
var respsO=resps['opinions']
theSections=resps['sections']
console.log(respsO)
opinionsTable.clear().rows.add(respsO).draw()

})
}).fail(function (xhr,e,r) {
//console.log(xhr)
})

})
$('#updateform').on('click',function (e) {
e.preventDefault()
if($('#num').val().length == 0 ){
alert('please select a valid record to edit')
}else {


$.ajax({
method: 'POST',
url: '/updateinfo',
headers: {
'X-CSRFToken': '{{csrf_token}}'
},
data: {
'te2chira_id': $('#theid').val(),
'opinion': $('#opinion').val()
}

}).done(function (msg) {
console.log(msg)
document.location = '/simpleInformation.html'
}).fail(function (xhr, e, r) {
console.log(xhr)
})
}
})
var opinionsTable=$('#opinions_table').DataTable({
searching: false, paging: false, info: false,
columns:[
{'title':'sec','data':'section',
"render":function(val,type,row,meta){
console.log('the Value is ',val)
if (type == 'set'){
console.log('doing here ')
row.section = val
row.section_display=sections[row.section]
row.section_filter=sections[row.section]
return
}else if (type === 'display',val) {
console.log('display')
return theSections[val];
}
else if (type === 'filter') {
console.log('filter',val)
return row.section_filter;
}
// 'sort', 'type' and undefined all just use the integer
return row.section;
}},
{'title':'opinion','data':'opinion'}
]
})
})

</script>
</body>

{% endblock %}

这是我需要修复的代码块:

<textarea name="field5" class="field-style" placeholder="opinion" id="opinion"></textarea>
<script type="text/javascript">

if($('#opinion').val() == null ){
window.setTimeout(function () {
location.href = " ";
}, 5000);
}
</script>

如果带有 id 选项的文本框有来自用户的输入,这意味着它不是空的,那么页面将不会刷新。否则,如果它为空,则页面必须每 5 秒刷新一次。

最佳答案

试试这个,默认情况下,页面将每 5 秒重新加载一次,但如果你填写输入 - 页面将停止重新加载,但是,如果你再次填写空输入 - 页面将每 5 秒重新开始重新加载,以避免这种情况,只需删除 if( $(this).val().length == '' ) {}

中的 myTimer

更新:

 // define timer that reload page every 5 seconds;
var myTimer = setInterval('window.location.reload()', 5000);
// input events
$('#opinion').on('keyup change', function(){
if( $(this).val().length == '' ) {
// if you leave input empty again - page start reloading again
myTimer = setInterval('window.location.reload()', 5000);
} else {
// if input not empty - clear interval and stop reload page
clearInterval(myTimer);
}
});

Fiddle

Screencast

关于javascript - 如果文本框不为空,则拒绝页面刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54088125/

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