0

我有两个与我的问题相关的不同代码。我只想将它们结合起来以正常工作。

我已经尝试了我所知道的所有可能的情况。我是 javascript 的初学者。

<script>
        function AllowAlphabet(e) {
            isIE = document.all ? 1 : 0
            keyEntry = !isIE ? e.which : event.keyCode;
            if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
                return true;
            else {

                alert('Alphabets Only...!!');
                return false;
            }
        }
    </script>

-------------------------
<script>
$(document).ready(function(){
  $('[data-toggle="popover"]').popover();   
});
</script>

<asp:TextBox runat="server" CssClass="form-control" data-toggle="popover" data-placement="right" title="Popover title" data-content="And here's some amazing content. It's very engaging. Right?" onkeydown="allLetter(inputtxt);" onkeypress="return AllowAlphabet(event)" placeholder="First Name"></asp:TextBox>

** 我想组合这些脚本。我想在第一个脚本的 else 部分运行第二个脚本。**

4

1 回答 1

0

也许像下面这样的东西会有所帮助。只需调用AllowAlphabetindocument.ready并按如下方式更新 else 语句。

function AllowAlphabet(e) {
    isIE = document.all ? 1 : 0
    keyEntry = !isIE ? e.which : event.keyCode;
    if (((keyEntry >= '65') && (keyEntry <= '90')) || ((keyEntry >= '97') && (keyEntry <= '122')) || (keyEntry == '46') || (keyEntry == '32') || keyEntry == '45')
        return true;
    else {
        $('[data-toggle="popover"]').popover();
        alert('Alphabets Only...!!');
        return false;
    }
}
$(document).ready(function(){
     AllowAlphabet();
});

但是,如果您想AllowAlphabet在代码中的其他地方调用,则无需将其包装在document.ready.

更新
您似乎正在使用onkeypress事件侦听器来调用该AllowAlphabet函数。在这种情况下,由于此时文件已准备好,document.ready因此不再需要使用。只需将$('[data-toggle="popover"]').popover();您的 else 语句放在上面。

于 2019-03-28T02:50:05.093 回答