jQuery.on(更改)上的特定ID [英] jquery .on(change) on specific ID

查看:78
本文介绍了jQuery.on(更改)上的特定ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我很累,在这里编写代码时,代码实际上正在正常工作! WTF:D

Sorry I was very tired and the code was actually working when writing here! WTF :D

我在同一页面上有许多不同的下拉菜单,并且当它们中的一个发生更改时,我不希望发出AJAX请求.

I have many different dropdowns on the same page and wan't to do an AJAX request when on of them changes.

如果我这样操作,它将起作用:

If I do it this way, it works:

  $(document).on('change', '#changeresponsibleuser', function(){
    var user = $(this).val();
    var partnerid = $(this).attr('name');
           $.ajax({
              type: "POST",
              url: "resources/dialogs/editResponsibleUser.php",
              data: {user: user,
                     partnerid: partnerid},
              success: function(msg){
                    $('.productmessage').html(msg).hide().fadeIn(500).fadeOut(4000);

                  },
                });
  });

但是我在页面上的所有下拉框中都花了不少钱.我不会做这样的事情:

But this trickers on all dropdown boxes I have on the page. I wan't to do something like this:

  $(document).ready(function(){
  $("#changeresponsibleuser").on('change',function(){
    var user = $(this).val();
    var partnerid = $(this).attr('name');
           $.ajax({
              type: "POST",
              url: "resources/dialogs/editResponsibleUser.php",
              data: {user: user,
                     partnerid: partnerid},
              success: function(msg){
                    $('.productmessage').html(msg).hide().fadeIn(500).fadeOut(4000);

                  },
                });

      });
  });

因此,它仅侦听具有该确切ID的下拉列表.但它不起作用,更改事件未触发.

So it ONLY listens to the dropdown with that exact ID. But it does not work, the on change event is not triggered.

仅在#changeresponsibleuser更改时如何触发事件?

How can I trigger the event only when #changeresponsibleuser changes?

标记看起来像这样:

<select id="changeresponsibleuser" name="2" class="responsible2 form-control form-group">

<option name="user" value="36" selected="selected">One user</option>
<option name="user" value="41">Another user</option>
<option name="user" value="40">yet another user</option>
</select>

提前谢谢!

推荐答案

$(function(){
 $("#changeresponsibleuser").on('change', function(){
   alert("Works");
 })
  
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="changeresponsibleuser" name="2" class="responsible2 form-control form-group">

<option name="user" value="36" selected="selected">One user</option>
<option name="user" value="41">Another user</option>
<option name="user" value="40">yet another user</option>
</select>

有效.您怎么说呢?

但这在我页面上的所有下拉框中都是骗人的

But this trickers on all dropdown boxes I have on the page

不.您必须使用类名而不是ID.看来您使用的是重复的ID.因此,它会触发具有重复ID的所有选择下拉列表.

No. You have to use class name instead id. It seems you are using duplicate ids. So it is triggering on all select dropdowns which has duplicate id.

$(document).on('change', '.changeresponsibleuser',function(){
   alert("Works");
 });

它将触发所有具有类changeresponsibleuser

这篇关于jQuery.on(更改)上的特定ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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