jQuery技术:jQuery只有在div中已存在时才会更改

我目前有一个带有IDdiv页面,其中包含一个yes和no按钮的ID 。 当选择按钮时,jQuery用它调用的add_song.php文件替换ID的信息。

当艺术家更改时,我在页面上的下拉列表中进行了实时change和实时add_song.php更改add_song.php的艺术家ID。 问题是当我选择是或否时,如果我更改艺术家名称,则加载add_song.php

当在change / add_song.php上更改艺术家后,在是或否按钮单击后加载add_song.php时,如何仅允许changeadd_song.php更改艺术家。

这是PHP:

 echo ''; echo ''; echo '
';

对不起,这里是jquery代码:

 $(document).ready(function(){ $("#add_new_song").live("click", function() { var artistid = $("#artist").val(); $("#display_add_new_song").load("php/add_song_new.php?var1=" + artistid); }); }); $(document).ready(function(){ $("#artist").change(function() { var artistid = $("#artist").val(); $("#display_add_new_song").load("php/add_song_new.php?var1=" + artistid); }); }); $(document).ready(function(){ $("#artist").keyup(function() { var artistid = $("#artist").val(); $("#display_add_new_song").load("php/add_song_new.php?var1=" + artistid); }); }); 

    您应该将keyup / change的事件绑定移动到.load 回调函数中 。 .load可能需要第二个参数,即在内容加载后立即执行的回调,允许您在加载后立即将事件绑定到#artist

    此外,您可以将文档准备好之后执行的JavaScript合并到一个块中,并且可以将要绑定的事件组合到相同的function中,这将删除脚本中的一些重复代码。

    考虑到您的代码,您应该将其更改为以下内容:

     $(document).ready(function(){ $("#add_new_song").live("click", function() { var artistid = $("#artist").val(); $("#display_add_new_song").load("php/add_song_new.php?var1=" + artistid, function(){ $("#artist").on("keyup change", function() { var artistid = $("#artist").val(); $("#display_add_new_song").load("php/add_song_new.php?var1=" + artistid); }); }); }); }); 

      以上就是jQuery教程分享jQuery只有在div中已存在时才会更改相关内容,想了解更多jQuery开发(异常处理)及jQuery教程关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/jquerytutorial/541351.html

      (0)
      上一篇 2021年1月3日
      下一篇 2021年1月3日

      精彩推荐