1

I use the following function on keyup event to redirect to another javascript function. Problem is it does'nt seem to fire, although it does bind the function to the textbox.

$(document).ready(function () {
EnablePickTargetButton();

//clear contents; use a delay because tinyMCE editor isn't always fully loaded on document.ready
var t = setTimeout(function () {
    if (typeof textEditorForCreate != 'undefined' && tinymce.editors.length > 0)
        tinyMCE.activeEditor.setContent('');
}, 300); 

var txtSearchUser = $('#txtSearchUser');
if(typeof txtSearchUser != 'undefined')
{
    $('#txtSearchUser').keyup(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();                
            searchUser();
        }
        else
            alert('cucu');
    });
}
});

Not even the alert shows up. Checking the html, I can see it doesn't add onkeyup to the textbox; The textbox is in a popup window hosted in a div on the form; But on document.ready it runs the function without error.

4

3 回答 3

5

在文档或最近的静态元素上尝试此委托。(如果元素是动态添加的)

$(document).on('keyup','#txtSearchUser',function(){
    //Code
});
于 2013-09-06T13:56:13.890 回答
0

有用 :

$(document).ready(function(){
    $('#txtSearchUser').keyup(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();                


        }
        else
            alert('cucu');
    });
});

http://jsfiddle.net/jMk5S/

检查您是否正确引用了 html 元素。也许您将 id 与类混合?

于 2013-09-06T13:42:34.923 回答
0

我编辑了您的代码并且正在工作:

问题出在您的文档就绪功能中,您有语法错误,下次在浏览器中检查控制台,看看有什么问题:

在这里演示

$(document).ready(function () {
//EnablePickTargetButton();

//clear contents; use a delay because tinyMCE editor isn't always fully loaded on document.ready
var t = setTimeout(function () {
    if ($('#textEditorForCreate').length != 0 && tinymce.editors.length > 0)
        tinyMCE.activeEditor.setContent('');
}, 300); 

if($('#txtSearchUser').length!=0)
{
    $('#txtSearchUser').keyup(function (e) {
        if (e.keyCode == 13) {
            e.preventDefault();                
            searchUser();
        }
        else
            alert('cucu');
    });
}
});
于 2013-09-06T13:46:40.420 回答