English 中文(简体)
如何防止有人利用 j和 Java本失去重点?
原标题:How to prevent an textfield losing focus using jQuery and JavaScript?

我有一个文本领域:<代码><input category=“text”/>,我想在失去重点时加以验证。 如果意见不有效,我想防止重点转向下一个要素,或者说要把重点放在无效的投入上,直到其有效为止。

我如何利用 j和 Java?

I have tried with this code, but it doesn t work (jsFiddle):

<!DOCTYPE html>
<html>
<head>
<script src="jquery-1.6.2.min.js"></script>
<script>
$(function() {
    $( .hello ).bind( focusout , function(e) {
        if(!isValid($(this).val())) {
            e.preventDefault();
            $(this).foucus();
        }
    });
});

    function isValid(str) {
        if(str === "hello") {
            return true;
        } else {
            return false;
        }
    }
</script>
</head>
<body>
Only valid content: <b>hello</b><br>
<input type="text" class="hello"/>
<button>Dummy</button>
</body>
</html>
最佳回答

这只是一个打字。 变化:

$(this).foucus();

:

$(this).focus();

此外,您不妨通过在文本箱上打电话select使用户更容易改正错误。 这样,他们就能够开始重新打字,改变价值:

$(this).focus().select();

http://jsfiddle.net/eu2Yc/“rel=“nofollow noreferer”


<>说明: 答案是解决眼前的问题,即提出的问题。 在更广泛的范围内,我确实同意其他人的看法,他们说,应该把用户锁定到一个领域。 这样做的更好办法是验证提交的全部形式,让用户看到所有问题,并一劳永逸地确定这些问题,而不是在整个上ug。

问题回答

活动应为<代码>blur。 你们最初的 j子有打字(foucus,而不是焦点)

正如一位评论家所说,来访者胜过这种行为。

http://jsfiddle.net/qdT8M/4/"rel=“nofollow> http://jsfiddle.net/qdT8M/4/

$("#input").focus();


$("#input").blur(function() {
    setTimeout(function() { $("#input").focus(); }, 0);
});

而不是<代码>$(本项)。 活动应为blur,而不是focusout

Edit: oh well,focus(>,也将发挥作用:





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签