Jquery in javascript function require two clicks

非 Y 不嫁゛ 提交于 2019-12-25 08:55:43

问题


I wrote a simpe inline edit using jquery. The plugin work very well, but i have a problem when i call the script within a javascript function, it require 2 click to activate the plugin. Does anyone know a way to solve this issue.. i want it in one click! Thanks in advance.

<a onclick="update(1)"> Let's update<a/>

  function update(id)
  {
  $("#edit" + id).kb_edit();   
  }

回答1:


If the functionality in the plugin requires the click event handler that you're setting up inside, then that means it won't be set up until you run .kb_edit().

So the first click runs .kb_edit(), which sets up the click handler.

Then the second click actually gets to trigger whatever was set up by the first click.




回答2:


well for starters you could clean it up a little by NOT using the onclick...

<a id="myAnchor">Let's update</a>

$(document).ready(function() {
   $("#myAnchor").click(function(){
        ///put your update code here including the kb_edit code
   });
});

or if you have a series of this you can use <a class="myAnchor">...</a> and change the jquery selector:

   $(".myAnchor").click(function(){


来源:https://stackoverflow.com/questions/6526906/jquery-in-javascript-function-require-two-clicks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!