Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果一个变量有一个值,我将如何连接一个字符串?_Javascript_Jquery - Fatal编程技术网

Javascript 如果一个变量有一个值,我将如何连接一个字符串?

Javascript 如果一个变量有一个值,我将如何连接一个字符串?,javascript,jquery,Javascript,Jquery,我试图根据输入是否有值来连接网址,例如,我只想从四个变量中得到两个值。awayVal或homeVal以及awayId或homeId。如果输入有一个值,我想获取该值和输入id,而不是另一个 然后我想将其连接成如下所示的内容: “+gameId+”/“+id+”/“+值” 做这件事的好方法是什么?抱歉,如果这有点让人困惑,请让我澄清一下,如果你不明白我在试图做什么 $('.wrapper').append('\ <div id="' + gameId + '" class="main-wrap

我试图根据输入是否有值来连接网址,例如,我只想从四个变量中得到两个值。awayVal或homeVal以及awayId或homeId。如果输入有一个值,我想获取该值和输入id,而不是另一个

然后我想将其连接成如下所示的内容:

“+gameId+”/“+id+”/“+值”

做这件事的好方法是什么?抱歉,如果这有点让人困惑,请让我澄清一下,如果你不明白我在试图做什么

$('.wrapper').append('\
<div id="' + gameId + '" class="main-wrapper col-lg-6 col-md-6 col-sm-12">\
    <div class="game-cards">\
        <div class="chart-container">\
            <canvas id="' + homeTeam + '" width="500" height="500"></canvas>\
        </div>\
        <div class="right-info">\
            <h4>' + awayTeam + '<br>' + " @ " + '<br>' + homeTeam + '</h4>\
            <h5 id="time-channel">' + gameDate + ' @ ' + gameTime + '<br>' + ' On ' + network + '</h5>\
            <div class="total-points-live">\
                <h5>Total Points Bet</h5>\
                <h5 id="point-total">' + pointTotal + '</h5>\
                <p>' + awayTeam + '</p>\
                <input class="bet-input-away" data-away-id="' + awayId + '" data-team-type="' + awayTeam + '" type="number" pattern="[0-9]*" name="betAmountAway" placeholder="Wager Amount">\
                <p>' + homeTeam + '</p>\
                <input class="bet-input-home" data-home-id="' + homeId + '" data-team-type="' + homeTeam + '" type="number" pattern="[0-9]*" name="betAmountHome" placeholder="Wager Amount">\
                <p class="bet-button" gameid="' + gameId + '">Click To Place Bet</p>\
            </div>\
        </div>\
    </div>\ ');

 $('.wrapper').on('click', '.bet-button', function() {
             var self = $(this);
             var gameId = self.attr('gameid');
             var awayVal = $('#' + gameId + ' input[name=betAmountAway]').val();
             var homeVal = $('#' + gameId + ' input[name=betAmountHome]').val();
             var awayId = $('#' + gameId + ' .bet-input-away').data('away-id');
             var homeId = $('#' + gameId + ' .bet-input-home').data('home-id'); // This is what sends the bet to the server. $.ajax({ url: "https://-------.islandshore.net/dbdata/bet/new/1/" + gameId + "/" + awayId + "/" + homeVal || awayVal + "", type: "get", success: function(response) { $('#' + gameId + ' input[name=betAmountHome]').val(''); //This resets the value box $('#' + gameId + ' input[name=betAmountAway]').val(''); //This resets the value box console.log("https://--------.islandshore.net/dbdata/bet/new/1/" + gameId + "/" + awayId + "/" + homeVal || awayVal + "") }, error: function(xhr) { console.log('xhr') } }); console.log(awayId); console.log(homeId); console.log(gameId); console.log(homeVal); console.log(awayVal); }); });
$('.wrapper').append('\
\
\
\
\
\
\
“+awayTeam+”
“+”@“+”
“+homeTeam+”\ “+gameDate+”@“+gameTime+”
“+network+”上的“+”\ \ 总积分赌注\ “+pointTotal+”\ “+awayTeam+”

\ \ “+主队+”

\ \

单击下注\ \ \ \ '); $('.wrapper')。在('click','.bet按钮',函数()上{ var self=$(这是); var gameId=self.attr('gameId'); var awayVal=$('#'+gameId+'input[name=betAmountAway]')。val(); var homeVal=$('#'+gameId+'input[name=betAmountHome]')。val(); var awayId=$('#'+gameId+'.bet输入away')。数据('away-id'); var homeId=$('#'+gameId+'.bet input home').data('home-id');//这是将赌注发送到服务器的内容。$.ajax({url:https://-------.islandshore.net/dbdata/bet/new/1/“+gameId+”/“+awayId+”/“+homeVal | | | awayVal+”,键入:“获取”,成功:函数(响应){$('#'+gameId+'input[name=betAmountHome]')。val('');//这将重置值框$('#'+gameId+'input[name=BetaMontaway]').val('');//这将重置值框console.log('https://--------.islandshore.net/dbdata/bet/new/1/“+gameId+”/“+awayId+”/“+homeVal | | awayVal+”)},错误:函数(xhr){console.log('xhr')});console.log(awayId);console.log(homeId);console.log(gameId);console.log(homeVal);console.log(awayVal);});});

您可以试试

var id = awayId || homeId;
var value = awayVal || homeVal;

var url = `https://--------.islandshore.net/dbdata/bet/new/1/${gameid}/${id}/${value}`

另外,您可以使用来避免使用所有这些
+

,因为您提到只有一个值是可能的,您可以这样做:

var id, value;

// If the awayVal is set, assign away info to id and value variables
if (awayVal) {
  id = awayId;
  value = awayVal;
}
// If the homeVal is set, assign home info to id and value variables
if (homeVal) {
  id = homeId;
  value = homeVal;
}

// If there is the possibility that none of the values (awayVal or homeVal) is set and the user can execute you need to check if they are valid
if (!value) {
    // handle error
}

// Build your url with id and value variable
var url = `http://yoururl.com/${gameId}/${id}/${value}`;

此外,在发送ajax调用之前,您可能需要检查/验证是否至少设置了一个值

您是否在询问如何连接或如何检查变量是否有值?因为这听起来像是您希望它知道如何检查变量是否有值的全部内容,为此,您可以使用例如var!==null,var!='un已定义',如果(变量),如果(变量!='')。我正在尝试查看变量是否有值,如果有,我需要该值,但我也需要该输入id,我需要两个单独的变量,以便正确连接URL。听起来您需要一组收音机来决定要使用什么值。或者收音机,只有2个输入even@charlietfl应该有办法做到这一点希望没有无线电。最好有一个绝对指示器来指示使用哪种组合。什么能决定or?BrettKessler var id=awayId | | homeId;意味着如果awayId有一个值,id将被设置为awayId,如果没有,则改为设置为homeId@BrettKessler你可以在这里读到更多关于这方面的内容,这样对va来说就更有效了r值,但既然hey id对这两个都存在,我不知道它对id是否有效,有什么建议吗?@BrettKessler,这取决于您希望哪一个具有更高的优先级。如果两者都有一个值,则需要从左到右的第一个值。您能进一步解释一下吗?我似乎无法让它工作。我不知道您如何设置vars id和value带有var id,value;我只是创建了它们,还没有赋值。它们在那些条件中设置之前是未定义的。如果这样做有效,gameid抛出了一个错误,因为我没有大写,它在和我作对哈哈,谢谢你的帮助!