You can use the conditional statement if to make a judgment. If the value of a variable or expression is empty, use the return keyword to terminate the execution of the function, for example:
function myFunction() {
var myVar = $('#myInput').val();
if (!myVar) { // 如果 myVar 的值为空,使用 ! 进行判断
return; // 终止函数的执行
}
// 如果 myVar 不为空,继续执行下面的代码
console.log('myVar is not empty');
}In this example, if the value of myVar is empty, the return keyword will terminate the execution of the function and the subsequent code will not be executed, otherwise a message will be output.