使用 val() 函数设置

发布于 2024-11-03 05:54:57 字数 508 浏览 3 评论 0原文

设置 select 元素的值时触发 change 事件的最简单和最好的方法是什么。

我预计执行以下代码

$('select#some').val(10);

$('select#some').attr('value',  10);

将导致触发更改事件,我认为这是非常合乎逻辑的事情。正确的?

嗯,事实并非如此。你需要通过这样做来触发change()事件

$('select#some').val(10).change();

,或者

$('select#some').val(10).trigger('change');

但我正在寻找一些解决方案,一旦某些javascript代码更改了select的值,就会触发change事件。

What is the easiest and best way to trigger change event when setting the value of select element.

I've expected that executing the following code

$('select#some').val(10);

or

$('select#some').attr('value',  10);

would result in triggering the change event, which i think is pretty logic thing to be done. Right?

Well, it isn't the case. You need to triger change() event by doing this

$('select#some').val(10).change();

or

$('select#some').val(10).trigger('change');

but i'm looking for some solution which would trigger change event as soon as select's value is changed by some javascript code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

抚笙 2024-11-10 05:54:57

我遇到了一个非常相似的问题,我不太确定您遇到了什么问题,因为您建议的代码对我来说非常有用。它立即(您的要求)触发以下更改代码。

$('#selectField').change(function(){
    if($('#selectField').val() == 'N'){
        $('#secondaryInput').hide();
    } else {
        $('#secondaryInput').show();
}
});

然后,我从数据库中获取值(这在表单上用于新输入和编辑现有记录),将其设置为所选值,并添加我缺少的部分以触发上述代码,”。 。

$('#selectField').val(valueFromDatabase).change();

因此,如果数据库中的现有值为“N”,它会立即隐藏表单中的辅助输入字段

I had a very similar issue and I'm not quite sure what you're having a problem with, as your suggested code worked great for me. It immediately (a requirement of yours) triggers the following change code.

$('#selectField').change(function(){
    if($('#selectField').val() == 'N'){
        $('#secondaryInput').hide();
    } else {
        $('#secondaryInput').show();
}
});

Then I take the value from the database (this is used on a form for both new input and editing existing records), set it as the selected value, and add the piece I was missing to trigger the above code, ".change()".

$('#selectField').val(valueFromDatabase).change();

So that if the existing value from the database is 'N', it immediately hides the secondary input field in my form.

天邊彩虹 2024-11-10 05:54:57

我发现的一件事(艰难的方式),
是你应该

$('#selectField').change(function(){
  // some content ...
});

在使用之前定义

$('#selectField').val(10).trigger('change');

 $('#selectField').val(10).change();

One thing that I found out (the hard way),
is that you should have

$('#selectField').change(function(){
  // some content ...
});

defined BEFORE you are using

$('#selectField').val(10).trigger('change');

or

 $('#selectField').val(10).change();
绿光 2024-11-10 05:54:57

直接的答案已经在重复的问题中: 为什么当我使用 val() 设置 select 的值时,jquery 更改事件没有触发?

正如您可能知道的那样,设置 select 的值不会触发触发change()事件,如果你正在寻找一个在通过JS更改元素值时触发的事件,那么没有这样的事件。

如果你真的想这样做,我想唯一的方法是编写一个函数来定期检查 DOM 并跟踪更改的值,但绝对不要这样做,除非你必须(不知道为什么你需要这样做)

添加了此解决方案:
另一种可能的解决方案是创建您自己的 .val() 包装函数,并在通过 .val() 设置值后让它触发自定义事件,然后当您使用您的 .val() 包装器设置

The straight answer is already in a duplicate question: Why does the jquery change event not trigger when I set the value of a select using val()?

As you probably know setting the value of the select doesn't trigger the change() event, if you're looking for an event that is fired when an element's value has been changed through JS there isn't one.

If you really want to do this I guess the only way is to write a function that checks the DOM on an interval and tracks changed values, but definitely don't do this unless you must (not sure why you ever would need to)

Added this solution:
Another possible solution would be to create your own .val() wrapper function and have it trigger a custom event after setting the value through .val(), then when you use your .val() wrapper to set the value of a <select> it will trigger your custom event which you can trap and handle.

Be sure to return this, so it is chainable in jQuery fashion

述情 2024-11-10 05:54:57

因为 jQuery 不会触发本机的更改事件,而只会触发它自己的更改事件。如果您在没有 jQuery 的情况下绑定事件,然后使用 jQuery 触发它,您绑定的回调将不会运行!

解决方案如下(100% 有效):

var sortBySelect = document.querySelector("select.your-class"); 
sortBySelect.value = "new value"; 
sortBySelect.dispatchEvent(new Event("change"));

As jQuery won't trigger native change event but only triggers its own change event. If you bind event without jQuery and then use jQuery to trigger it the callbacks you bound won't run !

The solution is then like below (100% working) :

var sortBySelect = document.querySelector("select.your-class"); 
sortBySelect.value = "new value"; 
sortBySelect.dispatchEvent(new Event("change"));
那请放手 2024-11-10 05:54:57

我将其分开,然后使用身份比较来决定下一步要做什么。

$("#selectField").change(function(){
  if(this.value === 'textValue1'){ $(".contentClass1").fadeIn(); }
  if(this.value === 'textValue2'){ $(".contentclass2").fadeIn(); }
});

I separate it, and then use an identity comparison to dictate what is does next.

$("#selectField").change(function(){
  if(this.value === 'textValue1'){ $(".contentClass1").fadeIn(); }
  if(this.value === 'textValue2'){ $(".contentclass2").fadeIn(); }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文