简体   繁体   English

通过单击按钮从复选框单击触发功能

[英]Trigger functions from checkbox on click by clicking on a button

I have a couple of checkboxes and a button.我有几个复选框和一个按钮。 When I click on checkbox - function is triggered.当我点击复选框时 - function 被触发。 This is the desired behavior but I want to trigger it by clicking on the button.这是所需的行为,但我想通过单击按钮来触发它。 I want to have the possibility to first select checkboxes (I tried with return false and event.preventDefault but these completely switch the selection off) and then by clicking the button - trigger functions from checkboxes.我希望有可能首先 select 复选框(我尝试使用 return false 和 event.preventDefault 但这些完全关闭了选择)然后通过单击按钮 - 从复选框触发功能。 Here is a link to jsfiddle:这是jsfiddle的链接:

http://jsfiddle.net/j93k2xns/6/ http://jsfiddle.net/j93k2xns/6/

So for instance: I can select 3 checkboxes (nothing should happen) and after I click the button - three alerts should appear.因此,例如:我可以 select 3 个复选框(什么都不应该发生),在我单击按钮后 - 应该出现三个警报。

The code:代码:

HTML: HTML:

<input type="checkbox" name='check[]' id="first">first</input>
<input type="checkbox" name='check[]'>second</input>
<input type="checkbox" name='check[]'>third</input>
<input type="checkbox" name='check[]'>fourth</input>

<input type="button" value="validate" id="val-button">

JS:记者:

var check_state;

$(document).on('click','input[name="check[]"]', function(e){
    if(check_state === true) {
        alert('a');
    } else {
         return false;   
    }

});
$(document).on('click','#val-button', function(){
   check_state = true; 
});
$(document).on('click','#val-button', function(){
   $( 'input[name="check[]"]' ).each(function( index ) {
       if($(this).is(':checked')) {
           alert("a");
           return true;
       }
    });
});

There are a few interpretations to his question. 他的问题有几种解释。 If I'm reading it correctly, he wants to bind an arbitrary function to the checkboxes. 如果我正确阅读,他想将任意函数绑定到复选框。 Clicking the button should fire this event. 单击该按钮将触发此事件。 This is how you can achieve that using custom events in jQuery: 这是使用jQuery中的自定义事件来实现的方法:

$(function () {
    $("input[name='check[]']").bind("myCustomButtonClick", function() { 
        if(this.checked) {
            alert('a');
        }
    });
})

$(document).on('click','#val-button', function(){
   $("input[name='check[]']").trigger("myCustomButtonClick");
});

And the associated jsfiddle: http://jsfiddle.net/3yf7ymos/ 以及相关的jsfiddle: http : //jsfiddle.net/3yf7ymos/

If you want to do something when the user checks a checkbox, add an event listener: 如果要在用户选中复选框时执行某些操作,请添加事件侦听器:

$('input[type="checkbox"]').click(function() {

   if ($(this).is(':checked')) {
      // do something
   }
});

If the idea is run a couple of functions after the inputs are checked by clicking on a button: 如果在通过单击按钮检查输入后运行该构思的几个功能,则:

function myFunction() {

    if ($('input[id="something"]:checked').length == 0) {
        // do something
    } else if ($('input[id="something_2"]:checked').length == 0) {
       // do something
    } 
//and so on..

}

$('#val-button').click(function() {

    myFunction();
});

I have a similar inquiry.我有一个类似的查询。 I have a number of check boxes.我有很多复选框。 Each checkbox is linked to a different URL that opens a PDF form.每个复选框都链接到一个不同的 URL,它会打开一个 PDF 表单。 I want my team to be able to select which forms they need by ticking the checkbox.我希望我的团队能够通过勾选复选框来 select 他们需要的 forms。 Once they have done that, I would like a button to trigger the opening of each form based on which check box is checked.一旦他们这样做了,我想要一个按钮来根据选中的复选框触发每个表单的打开。 I have it so the checkbox upon being checked opens the form right away but it is very distracting.我有它,所以复选框在被选中时会立即打开表格,但它非常分散注意力。 Its preferable they all get opened at once by a "button".最好通过“按钮”将它们全部同时打开。 Help.帮助。 I am quite new to JavaScript so may need additional clarity.我对 JavaScript 很陌生,所以可能需要进一步说明。

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

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