简体   繁体   English

jQuery onClick一次带有开/关开关

[英]jQuery onClick once with on/off switch

I am trying to figure out how to do something but can not figure out the correct terminology to do so. 我正在尝试找出如何做某事,但无法找出正确的用语。

What I am trying to do is have a textbox (#price) that when clicked once it will open up a pdf calculator that will then either prefill the textbox when completed or will then allow the user to enter the amount in. But I also want this to work if the textbox is "tabbed" over to also instead of the onClick. 我想做的是有一个文本框(#price),单击该文本框将打开一个pdf计算器,该计算器将在完成时预填充文本框,或者允许用户输入金额。但是我也想如果文本框也被“制表”到了onClick而不是onClick上,这将起作用。 (Maybe onBlur) Basically anytime that textbox is used I need it to work like that. (也许是onBlur)基本上,每次使用文本框时,我都需要像这样工作。 But how do I make the onClick know when the amount is ok to be entered or if the calculator needs to open? 但是,当可以输入金额或是否需要打开计算器时,如何使onClick知道呢?

What also makes this tricky is I need to have an On/Off switch basically a checkbox that when checked it allows that pop up pdf calculator and when its not checked it just ignores it and allows the price to be entered still. 棘手的是,我需要有一个On / Off开关,基本上是一个复选框,当选中它时,它会允许弹出的pdf计算器;而当未选中它时,它会忽略它并允许仍然输入价格。

Does anyone have any suggestions or pointers in how I can achieve this goal? 有人对我如何实现这一目标有任何建议或指示吗?

Bind event handler to focus event ( blur is for when control looses focus). 将事件处理程序绑定到focus事件( blur是控件失去焦点时的情况)。

$("#price").on({
 "focus": eventHandler
})

Then in your eventHandler() check if calculator needs to be invoked, by checking if it's already opened: $("#calculatorDiv").is(":visible") , and checking if your checkbox is 'checked': $("#checkboxId").is(':checked') , and depending on that open it. 然后在您的eventHandler() ,通过检查计算器是否已打开来检查是否需要调用计算器: $("#calculatorDiv").is(":visible") ,并检查您的复选框是否已“ checked”: $("#checkboxId").is(':checked') ,并取决于打开它。

The event(s) you are looking for is onFocus and onBlur. 您正在寻找的事件是onFocus和onBlur。 I would bind a function to the onFocus event that first checks if the corresponding checkbox has a "true" (or "checked") value, then continue if it does and do nothin if it doesn't. 我将一个函数绑定到onFocus事件,该事件首先检查相应的复选框是否具有“ true”(或“ checked”)值,然后继续(如果继续),不继续,不继续。

I'd create an example in jsfiddle for you if I wasn't answering this from my phone. 如果我不是从手机回答这个问题,我会在jsfiddle中为您创建一个示例。

1. A textbox (#price) that when click once it will open up a pdf calculator 1.一个文本框(#price),单击该文本框将打开一个pdf计算器

Use jQuery's click() handler or bind("click", ...) 使用jQuery的click()处理函数或bind("click", ...)

var $price = $("#price");
$price.click(function() {
    $("#pdf_calculator").fadeIn();
});

2. But I also want this to work if the textbox is "tabbed" over to also 2.但是,如果文本框被“制表”为还

Use the focus event to know when an input is active (ie, has been "tabbed" to). 使用focus事件可以知道输入何时处于活动状态(即已被“制表”为)。 Alternatively, the blur event can be used if you want to know when a user is "leaving" the input field. 另外,如果您想知道用户何时“离开”输入字段,可以使用blur事件。 ('blur' is the opposite of 'focus') (“模糊”与“焦点”相反)

$price.on("focus click", function() {
    $("#pdf_calculator").fadeIn();
});

3. But how do I make the onClick know when the amount is ok to be entered or if the calculator needs to open? 3.但是,当可以输入金额或是否需要打开计算器时,如何知道onClick?

Grab the amount typed in by the user, convert it to a numerical value, then perform your validation steps. 抓住用户键入的金额,将其转换为数值,然后执行验证步骤。

$price.on("focus click", function() {
    // Do some validation checking on the amount entered.
    var enteredValue = parseFloat($price.val());
    if (!isNaN(enteredValue) && enteredValue > 0) {
        $("#pdf_calculator").fadeIn();
    }
});

4. What also makes this tricky is I need to have an On/Off switch basically a checkbox that when checked it allows that pop up pdf calculator and when its not checked it just ignores it and allows the price to be entered still. 4.另一个棘手的问题是,我需要有一个On / Off开关,基本上是一个复选框,当选中它时,它会允许弹出的pdf计算器;而当未选中它时,它会忽略它并允许仍然输入价格。

Simply check that the checkbox is checked using jQuery's is(":checked") then combine the steps above, and your fully working code looks like this: 只需使用jQuery的is(":checked")来检查复选框是否已选中,然后结合以上步骤,即可正常工作的代码如下:

var $price = $("#price");

$price.on("focus click blur", function() {

    // your checkbox element
    var checkbox = $("#show_calculator");

    // Check if the checkbox is checked
    if (checkbox.is(":checked")) {

        // convert the entered string to a number
        // then validate it according to your needs
        var enteredValue = parseFloat($price.val());
        if (!isNaN(enteredValue) && enteredValue > 0) {

            // if all conditions are met, 
            // show the pdf calculator
            $("#pdf_calculator").fadeIn();
        }
    }
});

Click here to review a working jsfiddle of these ideas. 点击这里查看这些思想工作的jsfiddle。

As for the pdf form (and getting values in and out again of a pdf form) there isn't a straight-forward method that doesn't involve a 'hack' (that may or may not work across different browsers). 至于pdf表单 (以及从pdf表单获取值和再次获取值),没有一种不涉及“ hack”的简单方法(该方法可能会在不同的浏览器上工作,也可能无法工作)。 If the pdf only has ONE input, then you can capture the keyboard events on your form popup, and send them back to the HTML form (which is an ugly hack), but if this were my project, I would just convert the pdf functionality to javascript, and then you have all the freedom you need, and your calculator is 100% compatible with the rest of your application. 如果pdf仅输入一个,那么您可以捕获表单弹出窗口中的键盘事件,并将它们发送回HTML表单(这是一个丑陋的hack),但是如果这是我的项目,则只需转换pdf功能javascript,然后您便拥有了所需的全部自由,并且计算器与其余应用程序100%兼容。

Hope this helps! 希望这可以帮助!

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

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