简体   繁体   English

javascript和html onclick&onload函数

[英]javascript and html onclick & onload functions

How can I use html to fire a toggle function written in javascript for both page load and when I click on the checkbox? 当我单击复选框时,如何使用html来触发用javascript编写的切换功能?

Essentially what the function does is toggle a certain input box for the user. 本质上,该功能的作用是为用户切换特定的输入框。 I allow them to enter a custom price. 我允许他们输入自定义价格。 I want this box to show by default, and also to be toggled. 我希望此框在默认情况下显示,并且也要切换。 Am currently able to have just "onclick" work with this input but when I add the "onload" it does not load by default. 当前可以使用此输入进行“ onclick”操作,但是当我添加“ onload”时,默认情况下不会加载。 Yes, I can still click it to toggle it. 是的,我仍然可以单击它进行切换。

<input 
type="checkbox" 
id="item_use_custom_price_<?php echo $_item->getId() ?>" 
checked="checked" 
onload="toggleCustPrice(order, this);" 
onclick="toggleCustPrice(order, this);"
/>

I have the above input in a PHTML file (not really a php question). 我在PHTML文件中输入了上述内容(实际上不是php问题)。 The ID is pulling from PHP there can be many rows for the grid. 该ID是从PHP中提取的,网格中可能有很多行。

I do not think I can use the window.onload() function because in my JS script because I need to be able to pass the "order" and "this" parameters into the function so the proper row toggles. 我认为我不能使用window.onload()函数,因为在我的JS脚本中,因为我需要能够将“ order”和“ this”参数传递给该函数,以便正确切换行。

You asked in a comment about using data-load which was demonstrated in this SO answer and my answer would be yes. 您在评论中询问了有关使用数据加载的信息,这在SO答案中得到了证明,我的答案是肯定的。 There is other ways to handle this but with modern browsers now days this is the simplest way to accomplish what your looking to do. 还有其他方法可以解决此问题,但是如今使用现代浏览器,这是完成您的工作的最简单方法。

Assuming that you do not need to differentiate between toggles, meaning if you place multiple toggles on the page in the future you want them to all toggle, you could do something like this: 假设您不需要区分切换,这意味着如果将来在页面上放置多个切换,则希望它们都进行切换,则可以执行以下操作:

<!-- You will just grab this by its name -->
<input type="checkbox" ... data-name-you-want-here=""/>

If you need a possible way to differentiate these in the future you could use this approach: 如果您将来需要一种可能的方法来区分它们,则可以使用以下方法:

<!-- Add the unique ID you already have as the value -->
<input type="checkbox" ... data-name-you-want-here="<?php echo $_item->getId() ?>"/>

Now you just need to figure out how you will call this code and trigger the toggle. 现在,您只需要弄清楚如何调用此代码并触发切换即可。 You can use jQuery if you need backwards compatibility but really I think you'll be fine with vanilla JavaScript and querySelector() or querySelectorAll() . 如果需要向后兼容,则可以使用jQuery,但实际上,我认为使用香草JavaScript和querySelector()querySelectorAll()会很好。 If you know there is only one data selector on the page use: 如果您知道页面上只有一个数据选择器,请使用:

var element = document.querySelector('name-you-used-here');
/* Now you have the element in JavaScript so toggle it */

Again if you have multiple then use: 同样,如果您有多个,则使用:

var element = document.querySelectorAll('name-you-used-here');
/* Now you have the element in JavaScript, loop and toggle accordingly */

So in vanilla JavaScript pseudocode this is how I see it working. 因此,在普通JavaScript 伪代码中,这就是我看到的工作方式。 The function would be called simply by placing the call in the footer of your page or adding a call to it in some sort of document ready function: 只需将调用放置在页面的页脚中或将其添加到某种文档就绪函数中,即可调用该函数:

function doToggle(){
    var elem = [Do Query Selector Here]

    ...Your toggle code here.
    elem.id <--- If you need to pull out the ID

    ...loop and use your toggle code if you used querySelectorAll 
}

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

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