Phonegap 和 jQuery Mobile 事件一次触发两次

发布于 2024-12-26 13:59:00 字数 1604 浏览 0 评论 0原文

我在使用 PhoneGap 和 jQuery Mobile 时遇到一个事件触发两次的问题 在我的应用程序中的 bookdetailpage(bookdetailpage.html) 上。当我选择该复选框时,会触发一个事件并对 Web 服务进行一些更新。(代码中的updateBooksInMybooks()

我只想选择该复选框,并且更新必须在后台完成,仅此而已。如果选中该复选框,则应保持原样,否则应取消选中该复选框(正常情况下)。

但是,当我点击/检查复选框时,它似乎工作正常,但事件一次触发两次。

  1. 第一次(应该如此)当您点击/选中复选框时。
  2. 第二次,当我单击/点击屏幕上的任意位置时,它会再次触发。

就像它挂起并等待屏幕上的新点击/点击,这样它就可以再次触发。

这是代码:

...
<div data-role="page" data-theme="c" id="detailpage"  data-url="detailpage.html">
<div data-role="header" id="hdrBokDetaljer" name="hdrBokDetaljer"  data-theme="c" >
      ...
</div>
<div data-role="content" id="contentBokDetaljer" name="contentBokDetaljer" >
        ...        
 <div id="detailmybooks">
   <fieldset id="mybooksGroup" data-role="controlgroup">                    
       <input type="checkbox" name="checkInMybooks" id="checkInMybooks" class="custom" />
       <label for="checkInMybooks">My books</label>    
   </fieldset>               
 </div>            
</div>

<div data-role="footer" id="ftrBokDetaljer" name="ftrBokDetaljer" ></div>

<script type="text/javascript" charset="utf-8">
   $(document).delegate('#detailpage', 'pagecreate', function () {

      $('#checkInMybooks').attr("checked", detailCheckboxVar); 

   });

   $('#checkInMybooks').change(function () {

       //event.preventDefault();
       updateBooksInMybooks($('#detailbookid').html());

       return false;
   });
</script> 
</div>
...

I have a problem with PhoneGap and jQuery Mobile that an event fires twice
on a bookdetailpage(bookdetailpage.html) in my app.When i select the checkbox an event fires and do some update to a Web Service.(updateBooksInMybooks() in the code)

I only want to select the checkbox and update must be done in background,Nothing more.if checkbox is checked then it should be remain as it is or if its not it should be unchecked (as normal case).

But when i tap/check on the checkbox it seems working correctly but event fires twice at a time.

  1. First time (as it should) when you tap/check the checkbox.
  2. Second time it fire again when i click/tap anywhere on the screen.

it's like it hangs and waits for a new click/tap on the screen,So it can fire again.

Here is the code:

...
<div data-role="page" data-theme="c" id="detailpage"  data-url="detailpage.html">
<div data-role="header" id="hdrBokDetaljer" name="hdrBokDetaljer"  data-theme="c" >
      ...
</div>
<div data-role="content" id="contentBokDetaljer" name="contentBokDetaljer" >
        ...        
 <div id="detailmybooks">
   <fieldset id="mybooksGroup" data-role="controlgroup">                    
       <input type="checkbox" name="checkInMybooks" id="checkInMybooks" class="custom" />
       <label for="checkInMybooks">My books</label>    
   </fieldset>               
 </div>            
</div>

<div data-role="footer" id="ftrBokDetaljer" name="ftrBokDetaljer" ></div>

<script type="text/javascript" charset="utf-8">
   $(document).delegate('#detailpage', 'pagecreate', function () {

      $('#checkInMybooks').attr("checked", detailCheckboxVar); 

   });

   $('#checkInMybooks').change(function () {

       //event.preventDefault();
       updateBooksInMybooks($('#detailbookid').html());

       return false;
   });
</script> 
</div>
...

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

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

发布评论

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

评论(2

还如梦归 2025-01-02 13:59:00

老问题,但我有一个类似的问题,所以我发布这个希望它能帮助别人。当我正确等待 PhoneGap 的“deviceReady”事件触发时,点击和点击的双重触发消失了。如果我等待,点击和点击只会触发一次,但如果我不等待,那么它们就会触发两次。让它工作的方法如下:

添加到你的 body 标签

<body onload="onBodyLoad()">

,然后添加一些 JavaScript:

<script type="text/javascript">
   function onBodyLoad() {
      document.addEventListener("deviceready", onDeviceReady, false);
   }

   function onDeviceReady() {
      $(function() {
         // Do your stuff here, binding to elements, etc
      });
   }
</script>

Old question, but I had a similar problem so I post this in hope it will help somebody. The double firing of taps and clicks went away when I properly waited on the "deviceReady" event from PhoneGap to fire. If I waited on that, taps and clicks only fired once, but if I didn't wait, then they'd fire twice. Here's how to make it work:

Add to your body tag

<body onload="onBodyLoad()">

And then add some JavaScript:

<script type="text/javascript">
   function onBodyLoad() {
      document.addEventListener("deviceready", onDeviceReady, false);
   }

   function onDeviceReady() {
      $(function() {
         // Do your stuff here, binding to elements, etc
      });
   }
</script>
太阳公公是暖光 2025-01-02 13:59:00

尝试on('click')

举个例子:

$("#btn_back_home").on('click',function(event)
{
    $.mobile.changePage( "#mobile_page_main", {reverse: true,changeHash: false});
});

Try, on('click').

For an example:

$("#btn_back_home").on('click',function(event)
{
    $.mobile.changePage( "#mobile_page_main", {reverse: true,changeHash: false});
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文