简体   繁体   English

返回false后运行的Javascript函数

[英]Javascript function running after return false

In my code, when a select box is changed, a js function (ConfirmDelete) is called which asks the user if they are sure they want to change the select box.在我的代码中,当更改选择框时,会调用一个 js 函数 (ConfirmDelete),询问用户是否确定要更改选择框。 If the user selects no, it will return false and would normally stop the execution.如果用户选择否,它将返回 false 并且通常会停止执行。

My problem is that I also have another AJAX/JS function defined after this that is waiting for the select box to be changed and if it is, it sends details to a php script which adds a record into a mysql database.我的问题是,在此之后我还定义了另一个 AJAX/JS 函数,它正在等待更改选择框,如果是,它会将详细信息发送到 php 脚本,该脚本将记录添加到 mysql 数据库中。

I'd like to know if there is anyway that I can stop the AJAX/JS code from running after a user has selected No.我想知道在用户选择“否”后是否有任何方法可以阻止 AJAX/JS 代码运行。

The JS function JS函数

function ConfirmDelete()
{
  var x = confirm("Are you sure?");
  if (x)
  return true;
  else
return false;
}

The AJAX/JS function AJAX/JS 函数

$( ".dropdown" ).change(function() { 
    var request = new XMLHttpRequest();
var id = this.value;
var content = this.name;
request.open("POST", "samefile.php", true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () {
if(request.readyState == 4 && request.status == 200) {
    var return_data = request.responseText;
    alert (return_data);

}
}

request.send("id="+id+"&content="+content);

window.location.reload();
window.location.reload();       
}

 );

The HTML Select box HTML 选择框

<select class="dropdown" name="09:00-1-0-16-05-26" Onchange="return ConfirmDelete();">
<option class="co0es0sp1fd1oo0fs0" value="47">mike berry</option>
<option value="cancel-47">Cancel</option></select>

The order of the html is html的顺序是

  • JS Function defined in header头文件中定义的 JS 函数

  • HTML code HTML代码

  • AJAX/JS Script tag after AJAX/JS 脚本标签后

The AJAX/JS function AJAX/JS 函数

$( ".dropdown" ).change(function() { 
    if(!ConfirmDelete()) return;  // right here, use this to confirm
    var request = new XMLHttpRequest();
var id = this.value;
var content = this.name;
request.open("POST", "samefile.php", true);
request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
request.onreadystatechange = function () {
if(request.readyState == 4 && request.status == 200) {
    var return_data = request.responseText;
    alert (return_data);

}
}

request.send("id="+id+"&content="+content);

window.location.reload();
window.location.reload();       
}

 );

Adding onchange event directly in HTML does not solve the problem.直接在 HTML 中添加 onchange 事件并不能解决问题。 Use this instead;改用这个;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM