1

I have the following two functions which are fired on different events:

$('.CCB').change(function (event) {
        var matches = [];
        $(".CCB:checked").each(function () {
            matches.push(this.value);
        });
        alert(matches);

    });

which is called when a check box item is checked and

$('#textBox').keydown(function (e) {
            var code = e.keyCode ? e.keyCode : e.which;
            var st = document.getElementById("textBox").value
            if (code != 8) // if backspace is hit don't add it to the search term
            {
                st = st + String.fromCharCode(code);

            } 
            else
            {
                st = st.substr(0, st.length - 1)

            }

        });

which is fired when the user types in a text box. Can I unite them in some way so when any of the actions is fired (either check box check or text box keydown) to get both the array with check box values and the string from the text box, so after that I can perform some custom logic on them?

4

2 回答 2

2

您可以将这两个事件的逻辑放入函数中,然后调用这些函数。例如。

function checkbox(){
  // logic for your operations on checkboxes
}

function keydown(){
   // logic for your operations on keydown in textbox
}

然后在您的事件处理程序中

var resultFromCheckboxLogic = checkbox();
var resultFromKeydownLogic = keydown();
于 2013-07-22T11:30:04.723 回答
2

请看一下http://jsfiddle.net/2dJAN/92/

$('input[type=text], input[type=checkbox]').change(function(){
    var whole_array = []
    var tex_field_value = [];
    $('input[type=text]').each(function(){
        tex_field_value.push($(this).val())
    });
    var check_box_value=[];
    $('input[type=checkbox]').each(function(){
        if($(this).is(':checked') == true){
            check_box_value.push($(this).val())
        }
    });
    whole_array = tex_field_value+","+check_box_value
    alert(whole_array)
});

这将适用于输入字段的每次更改。

让我知道我是否理解您的要求是否正确。

于 2013-07-22T11:48:11.367 回答