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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3361742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 15:18:04  来源:igfitidea点击:

how to prevent to change selectbox value if i say no to confirm

jquerydrop-down-menu

提问by JustLearn

i m using a select box of country, when user select a country then add branch link appears and user add branches under that country, but when user want to change country then all branches regarding that country should be destryoed. before changing a country a confirm box appears and show warning..everything is working fine but

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

if i click 'Cancel' on confirm box then branches remains there but select box value changed to new country (even i click on cancel)

如果我在确认框上单击“取消”,则分支仍然存在,但选择框的值已更改为新的国家/地区(即使我单击取消)

i need that if user cancel to change country then select box value also would be previous country.

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

please tell how to do it with JQuery my code is given below

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

<form>
<table>
    <tr>
       <td >Select Country :</td>
       <td >
       <select name="country_id" id="country"  class="selectbox" title="Please select country" validate="required:true" onchange="javascript:showBranch();" >
         <option value="" >Select Country </option>
         <option value="00002" > Afghanistan</option>
         <option value="00054" > Croatia</option> 
         <option value="00060" > Dominica</option> 
         <option value="00062" > Ecuador</option> 
         <option value="00064" > El Salvador</option> 
         <option value="00067" > Estonia</option> 
         <option value="00099" > India</option> 
       </select>
       </td>
   </tr>
   <tr>
       <td>&nbsp;</td>
       <td>
        <div id="branches">
        <!-- Raw Branch Details-->
             <div><span><a title="First Branch" href="">First</a></span></div>
             <div><span><a title="Second Branch" href="">Second</a></span></div>
             <div><span><a title="Third Branch" href="">Third</a></span></div>                  
        </div>
        <div id="brancherror"></div>
       </td>
   </tr>
   <tr>
    <td>&nbsp;</td>
    <td align="left">
        <span id="branchLink" style="display:none" >
        <a href="#" class="thickbox" id="branchhref">Add Branch(es)</a></span>
    </td>
  </tr>
 </table>
 </form>

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();   
    }

回答by Nick Craver

You can just store the previous value and set it back if needed, like this:

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

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
});

Or use .data()as @Gabysuggests, like this:

或者.data()按照@Gaby 的建议使用,如下所示:

$("#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
});

回答by garish

Below solution worked for me. First of all onfocus is called. This event keeps the current value in a attribute "PrvSelectedValue". Secondly onchange is called, we set the previous value if user cancel the confirmation popup.return false is used to prevent the postback in ASP.NET.

以下解决方案对我有用。首先调用 onfocus 。此事件将当前值保留在属性“PrvSelectedValue”中。其次 onchange 被调用,如果用户取消确认,我们设置之前的值。返回 false 用于防止 ASP.NET 中的回发。

<select   
onfocus="this.setAttribute('PrvSelectedValue',this.value);" 

onchange="if(confirm('Do you want to change?')==false){ this.value=this.getAttribute('PrvSelectedValue');return false; }" 

></select>

回答by Weiks

The following is working for me (Chrome). Above not. In case of this.defaultValue, it goes to empty other than default selected text/value.

以下对我有用(Chrome)。以上没有。在 this.defaultValue 的情况下,除了默认选择的文本/值之外,它会变为空。

if(confirm('sure?')){
    //do sth;
}else{
    this.selectedIndex = 0; // if cancel the confirm window, option remains as unchanged.
}

回答by Kris Kibak

I'm posting this reply at the risk of ridicule because I'm admittedly pretty noob at this stuff (only been learning a month or two), but I was able to handle a similar situation (changing time zones) just doing this:

我冒着嘲笑的风险发布此回复,因为我承认我对这些东西很菜(只学习了一两个月),但我能够处理类似的情况(改变时区)只是这样做:

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

Then in my html I included data-originaltimezone="whatever" in the select. I had the class timezonedropdown for the select as well. Hope that helps! :)

然后在我的 html 中,我在选择中包含了 data-originaltimezone="whatever"。我也有选择的类 timezonedropdown 。希望有帮助!:)