简体   繁体   English

弹出窗口中的按钮的“ onclick”仅起作用一次

[英]“onclick” of a button in a popup works only once

I have an html/php webpage (the file is called searchresults.php) that imports jquery mobile. 我有一个html / php网页(该文件称为searchresults.php),可以导入jquery mobile。 When you enter the page, the url is usually something like 当您进入页面时,URL通常类似于

www.domain.com/searchresults.php?&sort="off"&max="5"

In this example sorting is off and only 5 items are displayed. 在此示例中,排序关闭,仅显示5个项目。 On that page is a button that opens a popup where I want the user to be able to change these settings. 在该页面上,有一个按钮会打开一个弹出窗口,我希望用户可以在其中更改这些设置。 I use the built-in jquery mobile popup. 我使用内置的jQuery移动弹出窗口。 On that popup you can toggle "sort" on/off and you can enter a new maximum. 在该弹出窗口上,您可以打开/关闭“排序”,也可以输入新的最大值。 On the popup is an "ok" button to confirm your new settings. 在弹出窗口中,是一个“确定”按钮,以确认您的新设置。 It looks like this: 看起来像这样:

<a href="" id="okbutton" data-role="button" onclick="sortAgain();">OK</a>

The sortAgain(); sortAgain(); function in javascript looks like this: javascript中的函数如下所示:

function sortAgain();
{
    //some code to get the necessary variables//
    ...

    //change the href of the button so you reload the page
    document.getElementById("okbutton").href = "searchresults.php" + "?keyword=" + var1 + "&sort=" + var2 + "&max=" + var3
 }

So, basically, right before the "ok" button navigates to another page, I set its href of the page where it should navigate too. 因此,基本上,就在“确定”按钮导航到另一个页面之前,我将页面的href设置为也应导航到的页面。

This scheme works, and the searchresults.php file is fetched again from the server and re-interpreted (with the new variables in the url). 此方案有效,并且再次从服务器获取了searchresults.php文件并重新解释了(URL中带有新变量)。

However, if i try to change the settings again after changing it once, the popup just does nothing! 但是,如果我尝试在更改一次后再次更改设置,则弹出窗口将无任何作用! In other words, the href of the ok button on the popup stays empty and the javascript function sortAgain() is not called. 换句话说,弹出窗口上的ok按钮的href保持为空,并且不调用javascript函数sortAgain()。 I don't understand why it calls the onclick method perfectly fine the first time, but then refuses to call it again? 我不明白为什么它第一次调用onclick方法会很好,但是随后拒绝再次调用它呢?

I assume it has something to do with the fact that the popup html code is an integral part of searchresult.php file, and that hrefing to the same page gives problems? 我认为这与以下事实有关:弹出html代码是searchresult.php文件的组成部分,并且hrefing到同一页面会带来问题吗? The popup is pure html, no php is involved in the popup code. 弹出窗口是纯html,弹出代码中不涉及php。 and again, it works fine the first time. 再次,它第一次运行良好。

You should check out how to attach events via JavaScript. 您应该查看如何通过JavaScript附加事件。 See here: javascript attaching events 参见此处: javascript附加事件

You need to use the "pageinit" event when setting up click handlers in JQM: http://api.jquerymobile.com/category/events/ 在JQM中设置点击处理程序时,您需要使用“ pageinit”事件: http ://api.jquerymobile.com/category/events/

Here is an example of how to bind click handlers when the page is first loaded. 这是一个在页面首次加载时如何绑定单击处理程序的示例。

$( document ).on( "pageinit", "#that-page", function() {
    $('#okbutton').on( "click", "#that-page", function( e ) { 
        $(this).attr("href", "searchreslts.php");
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM