简体   繁体   English

如果输入字段为空,则删除或忽略变量

[英]Remove or Ignore Variable if Input Field is Empty

I am currently trying to construct an URL for an API based on the input that users type on the query form using JavaScript.我目前正在尝试根据用户使用 JavaScript 在查询表单上键入的输入为 API 构建 URL。 The input of the text field is saved in a variable that is further joint together as a final URL.文本字段的输入保存在一个变量中,该变量进一步连接在一起作为最终 URL。 Nevertheless, not all input fields will be used all the time.然而,并非所有输入字段都会一直使用。

I was wondering how can you ignore that particular variable if its value is empty.我想知道如果它的值是空的,你怎么能忽略那个特定的变量。

As I explained, I have a set of input fields:正如我所解释的,我有一组输入字段:

<input id="whoValue"  type="text" value=""   placeholder="Who">    
<input id="whenStart" type="text" value="" placeholder="Start Date">
<input id="whenEnd" type="text" value="" placeholder="End Date">
<input id="whatValue"  type="text" value="" placeholder="What">
<button id="searchBtn">Search</button></br>
<input id="result" type="text" value="" placeholder="Resulting URL">

Then when the button is pressed, JavaScript joins the input fields even if their content is empty.然后当按钮被按下时,JavaScript 会加入输入字段,即使它们的内容是空的。

$('#searchBtn').click(function () {

    var url = 'http://particularURLof/';
    var who = "…WHO" + $('#whoValue').val();
    var what = "…WHAT"+ $('#whatValue').val();
    var when = "…YEAR[" + $('#whenStart').val() + "+TO+" + $('#whenEnd').val() + "]";
    var moreUrl = '…url End';

    var resultUrl = url + who + what + when + moreUrl;
    $("#result").val(resultUrl);

});

Therefore, as mentioned above, I am trying to construct that resultUrl only with the variables that hold a value in the input field .因此,如上所述,我试图仅使用在输入字段保存值变量来构造该resultUrl

Here is a JSFiddle of what I have as well.这是我所拥有的JSFiddle

short if else like:短,如果还喜欢:

var result = if_this_is_true ? true : false;

to give you an example with your code给你一个例子,你的代码

$('#searchBtn').click(function () {

    var url     = 'http://particularURLof/';
    var who     = $('#whoValue').val().length > 0   ? "…WHO" + $('#whoValue').val() : '';
    var what    = $('#whatValue').val().length > 0  ? "…WHAT"+ $('#whatValue').val() : '';
    var moreUrl = '…url End';

    var resultUrl = who + what + moreUrl;
    $("#result").val(resultUrl);

});

http://jsfiddle.net/n1jtrpmv/1/ http://jsfiddle.net/n1jtrpmv/1/

$('#searchBtn').click(function () { $('#searchBtn').click(function () {

var url = 'http://particularURLof/';
var who = $('#whoValue').val();
var what = $('#whatValue').val();
var whenStart = $('#whenStart').val();
var whatEnd = $('#whenEnd').val(); 
var moreUrl = "endOfUrl";

if( (who != '') && (what != '') && (whenStart != '') && (whenEnd != '') ) {
   var resultUrl = url + who + what + whenStart + whenEnd + moreUrl;
} 
else if( (who == '') && (what != '') && (whenStart != '') && (whenEnd != '') ) {
   var resultUrl = url + what + whenStart + whenEnd + moreUrl;
} 
else if( (who != '') && (what == '') && (whenStart != '') && (whenEnd != '') ) {
   var resultUrl = url + who + whenStart + whenEnd + moreUrl;
}
else if( (who != '') && (what != '') && ((whenStart == '') || (whenEnd == '')) ) {
   var resultUrl = url + who + what + moreUrl;
}

$("#result").val(resultUrl);

}); });

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM