我的复选框值是在取消选中而不是检查时计算的

时间:2015-08-17 06:49:06

标签: javascript jquery html checkbox

以下是我的代码。我想计算复选框的值。我想要的功能就像,当我选择' #selectall'复选框,它选择所有复选框并计算其值。但是在这段代码中,它正在计算未选中' #selectall'复选框。我该如何解决这个问题?



I fixed the issue. Please see my solution below:

1. First declare a global varibale called "activeFileld"
@property(nonatomic,strong)id activeFiled;

2. Create a method called "registerForKeyboardNotifications"
- (void)registerForKeyboardNotifications 
{
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillShow:)
                                                     name:UIKeyboardWillShowNotification object:nil]; //Posted immediately prior to the display of the keyboard

        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(keyboardWillHide:)
                                                     name:UIKeyboardWillHideNotification object:nil]; //Posted immediately prior to the dismissal of the keyboard.
}

3. Called the above method in viewWillAppear:

-(void)viewWillAppear:(BOOL)animated{

  [super viewWillAppear:animated];
  //Register kryboard Notification
   [self registerForKeyboardNotifications];
}
4. Call the Delegate method for UitextFieldd Or UitextView

- (void)textFieldDidBeginEditing:(UITextField *)sender {

        self.activeField = sender;
}
- (void)textFieldDidEndEditing:(UITextField *)sender{
        self.activeField = nil;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    // save the text view that is being edited
    _notes = textView.text;

}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    // release the selected text view as we don't need it anymore
    _activeField = nil;
}

5.

- (void)keyboardWillShow:(NSNotification *)notification
{

    if([_activeField isKindOfClass:[UITextField class]]) {

        NSDictionary* info = [notification userInfo];
        NSLog(@"Dictionary %@",info);
        CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        kbRect = [self.view convertRect:kbRect fromView:nil];

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;

        CGRect aRect = self.view.frame;
        aRect.size.height -= kbRect.size.height;

        UITextField *textField = (UITextField*)_activeField;
        if (!CGRectContainsPoint(aRect, textField.frame.origin) ) {
            [self.tableView scrollRectToVisible:textField.frame animated:YES];
        }
    }else if([_activeField isKindOfClass:[UITextView class]]) {

        NSDictionary* info = [notification userInfo];
        NSLog(@"Dictionary %@",info);
        CGRect kbRect = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue];
        kbRect = [self.view convertRect:kbRect fromView:nil];

        UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbRect.size.height, 0.0);
        self.tableView.contentInset = contentInsets;
        self.tableView.scrollIndicatorInsets = contentInsets;

        CGRect aRect = self.view.frame;
        aRect.size.height += kbRect.size.height;

        UITextView *activeTextView = (UITextView*)_activeField;
        if (!CGRectContainsPoint(aRect, textField.superview.superview.frame.origin) ) {
            [self.tableView scrollRectToVisible:activeTextView.superview.superview.frame animated:YES];

       }


   }





}

// Called when the UIKeyboardWillHideNotification is received
- (void)keyboardWillHide:(NSNotification *)aNotification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    self.tableView.contentInset = contentInsets;
    self.tableView.scrollIndicatorInsets = contentInsets;
}

from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        menu = self.menuBar().addMenu(self.tr('View'))
        action = menu.addAction(self.tr('New Window'))
        action.triggered.connect(self.handleNewWindow)


    def handleNewWindow(self):
        window = QtGui.QMainWindow(self)
        window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        window.setWindowTitle(self.tr('New Window'))

        window.show()

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.resize(300, 300)
    window.show()
    sys.exit(app.exec_())




2 个答案:

答案 0 :(得分:0)

我删除了这个:

$(document).ready(function(){
    $('.pop').popover({content: "Other Expenses 200 <br> Other Expenses 200 <br>Other Expenses 200 <br> ", html: true, placement: "right", trigger: "focus"}); 
});

将您的代码复制/粘贴到jsfiddle.net

一切似乎都有效......你的问题在哪里...你的代码正在检查&amp;取消选中...!

http://jsfiddle.net/sgvwczjq/1/

答案 1 :(得分:0)

    Finally I found the problem it was because of I closed the $(document).ready(function() before the  execution 

    $(document).ready(function () {


        // Select all
        $('#selecctall').click(function (event) { //on click 
            if (this.checked) { // check select status
                $('.checkbox1').each(function () { //loop through each checkbox
                    this.checked = true; //select all checkboxes with class "checkbox1"               
                });
            } else {
                $('.checkbox1').each(function () { //loop through each checkbox
                    this.checked = false; //deselect all checkboxes with class "checkbox1"                       
                });
            }
        });

    **** // actually i closed the function here this should be at the end 

    //Addition for checked value
    $('input').click(function () {
        var sum = 0;
        $('input[type=checkbox]:checked').each(function () {
            sum += Number($(this).val());
        });
        console.log(sum);
        $("#sum").html(sum);
    });

});