Javascript 如何为两个';价值';如果';价值';平等
我想在值相同时设置警报。并且所选的值返回到以前的值 这是我的HTML:Javascript 如何为两个';价值';如果';价值';平等,javascript,jquery,html,Javascript,Jquery,Html,我想在值相同时设置警报。并且所选的值返回到以前的值 这是我的HTML: $(“to”).val(“BGK”); var fromCity=“”; var-toCity=“”; $('#from,#to')。更改(函数(){ var fromCityVal=$(“#from”).val(); var toCityVal=$(“#to”).val(); if(fromCityVal==toCityVal){ 警告(“出发城市可能与目的地城市不同!”); } 否则{ //问题是,我该怎么办? $(
$(“to”).val(“BGK”);
var fromCity=“”;
var-toCity=“”;
$('#from,#to')。更改(函数(){
var fromCityVal=$(“#from”).val();
var toCityVal=$(“#to”).val();
if(fromCityVal==toCityVal){
警告(“出发城市可能与目的地城市不同!”);
}
否则{
//问题是,我该怎么办?
$(“#from”).val();
}
});代码>
登巴萨
曼谷
莫斯科
登巴萨
曼谷
莫斯科
必须将以前的值存储为备份。您可以通过焦点事件来实现:
$("#to").val("BGK");
var item = '';
var previous = '';
$('#from,#to').on('focus', function () {
previous = this.value;
item = this;
}). change(function(){
var fromCityVal = $("#from").val();
var toCityVal = $("#to").val();
if (fromCityVal == toCityVal){
alert("city of origin may not be the same as the destination city!");
$(item).val(previous);
} else {
previous = this.value;
$("#from").val();
}
})
这是工作小提琴:
$(“to”).val(“BGK”);
var项目=“”;
var先前=“”;
$('#from,#to')。关于('focus',函数(){
previous=此值;
项目=本;
}). 更改(功能(){
var fromCityVal=$(“#from”).val();
var toCityVal=$(“#to”).val();
if(fromCityVal==toCityVal){
警告(“出发城市可能与目的地城市不同!”);
$(项目).val(以前);
}否则{
previous=此值;
}
})
登巴萨
曼谷
莫斯科
登巴萨
曼谷
莫斯科
这是修复方法
$("#to").val("BGK");
var fromCity; // have global variables to backup your selection in case of same values
var toCity;
$('#from,#to').on('focus', function() {
fromCity = $("#from").val(); // on focus of the dropdown set the values into global variables
toCity = $("#to").val();
});
$('#from,#to').change(function() {
var fromCityVal = $("#from").val();
var toCityVal = $("#to").val();
if (fromCityVal == toCityVal) {
alert("city of origin may not be the same as the destination city!");
// from and to are same so reset the selection to its previous state.
$("#from").val(fromCity);
$("#to").val(toCity);
}
});
请按预期查找工作小提琴-
另一种选择:
$("#to").val("BGK");
var $fromCityValOld = $("#from").val();
var $toCityValOld = $("#to").val();
$('#from,#to').change(function(){
$fromCityVal = $("#from").val();
$toCityVal = $("#to").val();
if ($fromCityVal == $toCityVal){
alert("city of origin may not be the same as the destination city!");
$("#from").val($fromCityValOld);
$("#to").val($toCityValOld);
} else {
$fromCityValOld = $fromCityVal;
$toCityValOld = $toCityVal;
}
});
在页面加载时处理,获取变量中的当前选择值,并在发出警报后使用此值映射选择框。FIDLE按预期工作!预期的行为是什么?我没有理解你的意思,而且所选的值返回到以前的值这意味着什么?我想他是指Else
而不是,
我想他是指当选择重复值时,所选的选项将重置为以前的值。哦,我必须存储以前的值。谢谢@megavexusyes,这是工作,谢谢@Reddy谢谢你的评论行。那是simple@dediwibisono很高兴我帮了忙。快乐的溺爱看起来不错,这就是我需要的,完美!谢谢你@Jose GalloYou wellcome:)@dedi wibisono祝你今天愉快
$("#to").val("BGK");
var $fromCityValOld = $("#from").val();
var $toCityValOld = $("#to").val();
$('#from,#to').change(function(){
$fromCityVal = $("#from").val();
$toCityVal = $("#to").val();
if ($fromCityVal == $toCityVal){
alert("city of origin may not be the same as the destination city!");
$("#from").val($fromCityValOld);
$("#to").val($toCityValOld);
} else {
$fromCityValOld = $fromCityVal;
$toCityValOld = $toCityVal;
}
});