jQuery 防止更改选择 [英] jQuery prevent change for select

查看:29
本文介绍了jQuery 防止更改选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果某个条件适用,我想防止选择框被更改.这样做似乎不起作用:

I want to prevent a select box from being changed if a certain condition applies. Doing this doesn't seem to work:

$('#my_select').bind('change', function(ev) {
  if(my_condition)
  {
    ev.preventDefault();
    return false;
  }
});

我猜这是因为此时所选的选项已经更改.

I'm guessing this is because by this point the selected option has already changed.

还有什么其他方法可以做到这一点?

What are other ways of doing this?

推荐答案

试试这个:

http://jsfiddle.net/qk2Pc/

var my_condition = true;
var lastSel = $("#my_select option:selected");

$("#my_select").change(function(){
  if(my_condition)
  {
    lastSel.prop("selected", true);
  }
});

$("#my_select").click(function(){
    lastSel = $("#my_select option:selected");
});

这篇关于jQuery 防止更改选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆