如何根据var是否有值来连接字符串?

时间:2017-12-16 01:40:51

标签: javascript jquery

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

然后我想把它连接成看起来像这样的东西:

https://--------.islandshore.net/dbdata/bet/new/1/" + gameId +" /" + id +" /" + value

这样做的好方法是什么?对不起,如果这有点令人困惑,请让我澄清一下,如果你不理解我想做什么。

$('.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); }); });

2 个答案:

答案 0 :(得分:0)

你可以尝试

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

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

另外,您可以使用template strings来避免使用所有+

答案 1 :(得分:0)

正如您所提到的,只有一个值可以实现:

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调用之前检查/验证是否至少设置了其中一个值