传递false时,html方法不输出“false”

时间:2018-05-28 15:14:35

标签: javascript jquery html

我正在编写一个简单的扩展来验证电子邮件地址,但在处理扩展程序中的bool值时会遇到一些奇怪的行为。

从片段中你可以看到boolOne和boolThree,它应该读为false但是为空。是否有理由在虚假时不附加“假”,就像“真实”一样?

如果我使用String()它可行,那么这是一个解决方案。

$(document).ready(function (){
    $("#buttonValidate").click(function () {
        $("#boolOne").html($("#emailOne").IsEmail());
        $("#intOne").html(ToInt($("#emailOne").IsEmail()));
        $("#boolTwo").html($("#emailTwo").IsEmail());
        $("#intTwo").html(ToInt($("#emailTwo").IsEmail()));
        $("#boolThree").html($("#emailThree").IsEmail());
        $("#intThree").html(ToInt($("#emailFive").IsEmail()));

        console.log({ One: $("#emailOne").IsEmail() });
        console.log({ Two: $("#emailTwo").IsEmail() });
        console.log({ Three: $("#emailThree").IsEmail() });

        function ToInt(val) {
            let result = 0;

            if (val) {
                result = 1;
            }

            return result;
        }
    });
});

$.fn.IsEmail = function () {
    var result = false;
    var val = this.val();
    var reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

    if (reg.test(val)) {
        result = true;
    }

    return result;
}
p.button-wrap {
  padding: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
        <h1>IsEmail</h1>

        <input type="text" id="emailOne" value="error" /> - bool: <span id="boolOne"></span>, int: <span id="intOne"></span>
        <br />
        <input type="text" id="emailTwo" value="valid@valid.no" /> - bool: <span id="boolTwo"></span>, int: <span id="intTwo"></span>
        <br />
        <input type="text" id="emailThree" value="error@error" /> - bool: <span id="boolThree"></span>, int: <span id="intThree"></span>
        <p class="button-wrap">
            <input type="button" id="buttonValidate" value="Validate" />
        </p>
    </div>

1 个答案:

答案 0 :(得分:-1)

作为一种变通方法,您可以使用"" + boolean强制将其强制转换为字符串,而"false"将返回false而不是$(document).ready(function() { $("#buttonValidate").click(() => { ["One","Two","Three"].forEach( num => { let isEmail = $(`#email${num}`).IsEmail() $(`#bool${num}`).html("" + isEmail); $(`#int${num}`).html(isEmail ? 1 : 0); }) }); }); const reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; $.fn.IsEmail = function() { return reg.test(this.val()) }

另外,我冒昧地重写你的代码;)

p.button-wrap {
  padding: 18px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
  <h1>IsEmail</h1>

  <input type="text" id="emailOne" value="error" /> - bool: <span id="boolOne"></span>, int: <span id="intOne"></span>
  <br />
  <input type="text" id="emailTwo" value="valid@valid.no" /> - bool: <span id="boolTwo"></span>, int: <span id="intTwo"></span>
  <br />
  <input type="text" id="emailThree" value="error@error" /> - bool: <span id="boolThree"></span>, int: <span id="intThree"></span>
  <p class="button-wrap">
    <input type="button" id="buttonValidate" value="Validate" />
  </p>
</div>
lead_researcher = models.ForeignKey(related_name='lead_researcher')
participating_researcher = models.ManyToManyField(related_name='participating_researcher')