如果我说不确认,如何防止更改选择框值

我使用一个国家/地区的选择框,当用户选择一个国家/地区时,会出现添加分支链接,用户在该国家/地区下添加分支机构,但是当用户想要更改国家/地区时,则该国家/地区的所有分支都应该被删除。 在更改国家之前,会出现一个确认框并显示警告..但是一切正常

如果我点击确认框中的“取消”,那么分支仍然在那里,但选择框值更改为新国家(即使我点击取消)

我需要,如果用户取消更改国家/地区,那么选择框值也将是以前的国家/地区。

请告诉我如何使用JQuery,我的代码如下

Select Country : Select Country Afghanistan Croatia Dominica Ecuador El Salvador Estonia India
 
  Add Branch(es)
function showBranch() { var countryid = jQuery('#country').val(); if (jQuery("#branches>div").size()) { if (confirm('If country has been changed then data corresponding to present branches will lost.Do you want to continue?')) { jQuery('#branches').html(''); } jQuery('#branchhref').attr({href: "index.php?option=com_advertise&view=createbranch&format=raw&country=" + countryid + "&KeepThis=true&TB_iframe=true&height=500&width=900",title: 'Add Branch'}); } else { jQuery('#branchhref').attr({href : "index.php?option=com_advertise&view=createbranch&format=raw&country=" + countryid + "&KeepThis=true&TB_iframe=true&height=500&width=900",title:'Add Branch'}); return true; } jQuery('#branchLink').show(); }

您可以只存储以前的值并在需要时将其设置回来,如下所示:

 var countryVal; $("#country").change(function() { var newVal = $(this).val(); if (!confirm("Are you sure you wish to destroy these country branches?")) { $(this).val(countryVal); //set back return; //abort! } //destroy branches countryVal = newVal; //store new value for next time }); 

或者像@Gaby建议的那样使用.data() ,如下所示:

 $("#country").change(function() { var newVal = $(this).val(); if (!confirm("Are you sure you wish to destroy these country branches?")) { $(this).val($.data(this, 'val')); //set back return; //abort! } //destroy branches $.data(this, 'val', newVal); //store new value for next time }); 

以下解决方案适合我。 首先调用onfocus。 此事件将当前值保存在属性“PrvSelectedValue”中。 其次调用onchange,如果用户取消确认弹出,我们设置前一个值.return false用于阻止ASP.NET中的回发。

  

我发布这个回复的风险是嘲笑,因为我对这个东西很有说服力(只学了一两个月),但我能够处理类似情况(改变时区)这样做:

 $(".timezonedropdown").change(function() { var newVal = $(this).val(); if(!confirm("Are you sure?")) { $(this).val($(this).closest('select').attr("data-originaltimezone")); } }); 

然后在我的html中,我在select中包含了data-originaltimezone =“whatever”。 我也选择了timezonedropdown类。 希望有所帮助! 🙂