简体   繁体   English

如何从javascript函数和值返回true?

[英]How do I return true from a javascript function along with a value?

Hello guys i am using a function to detect the image extension. 大家好,我正在使用检测图像扩展的功能。

<body>
<input id="imgExtension" type="text" />
<input id="myFile" type="file" />
<script>
function checkExt(x){
var y = x.split('.').pop();
if (y != "jpg" && y != "png") {
    return false;
}

else {
    // i want to return *y*[image extension] from here
    return true;
}
}

$(function(){
$("#myFile").change(function(){
var x = $(this).val;
    if(checkExt(x)){
       // i want to put [image extension] here in #imgExtension
       $('#imgExtension').val();
    }

    else  {
       alert("Error, invalid image extension");
    }
}    
}
</script>
</body>

Recently, I was just checking the image extension and returning true or false but i want to get the extension as well because I need to parse it to my PHP script. 最近,我只是检查图像扩展名并返回truefalse,但我也想获取该扩展名,因为我需要将其解析为我的PHP脚本。

Instead of return true or false you can return just the extension or null if there is no extension at all. 您可以只返回扩展名,而不返回true或false,如果根本没有扩展名,则可以返回null。

In other hand you can create one js object that contains the two values, but i would go for the first. 另一方面,您可以创建一个包含两个值的js对象,但我会优先考虑。

The easiest way to return more than one value is to return an object. 返回多个值的最简单方法是返回一个对象。

return { value: true, extension: '.jpg' };

You can then access the value returned as a normal object. 然后,您可以访问作为普通对象返回的值。

You cannot return two things (unless you use an array). 您不能返回两件事(除非您使用数组)。 I suggest that you return either the extension or null. 我建议您返回扩展名或null。 Because of something called falsfy in JavaScript, calling if(checkExt(x)) will evalulate to false for null and true for jpg or png. 由于JavaScript中称为falsfy的东西,如果将if(checkExt(x))调用为null,则将其评估为null;对于jpg或png,则将其评估为true。

You dont need to return 2 values. 您不需要返回2个值。 checkExt works as it should if you just return the value, or nothing at all. 如果您只返回值,或者什么都不返回,则checkExt可以正常工作。 The cleanest way to do things like this is with a ternary statement (google it, easy to learn, will save hours of your life) 做这样的事情的最干净的方法是使用三元语句(使用谷歌语言,易于学习,可以节省您的生命)

function checkExt(x){
  var y = x.split('.').pop();
  return y != "jpg" && y != "png" ? false : y;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 JavaScript 中的 Handler 函数获取返回值? - How do I get a return value from a Handler function in JavaScript? 如何将值从 JavaScript 中的函数返回到 HTML 中的文本框 - How do I return a value from a function in JavaScript to a textbox in HTML 如何从此函数返回值 - How do I return value from this function 返回javascript函数的true / false值,然后根据它做一些东西 - Return true/false value of a javascript function and then do stuff based on that 等待异步函数返回true或false - 如何检查返回值? - Waiting for async function to return true or false - how do I check return value? 如何从javascript函数返回一个变量和promise? - How to return a variable along with the promise from a javascript function? 如何创建函数以在JavaScript插件中返回值? - How do i create a function to return a value in a javascript plugin? 如何在JavaScript中传播嵌套异步函数的返回值 - How do I propagate a return value from a nested asynchronous function in javascript 如何通过Awesomium库从javascript函数返回值? - How do I return value from javascript function through Awesomium library? 如何将 function 中的值返回到 javascript 中的变量中,然后使用 HTML 显示它? - How do I return a value from a function into a variable in javascript then display it using HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM