在变更事件上触发jquery帖子

时间:2017-09-03 17:29:19

标签: jquery

我有一个选择框,用于选择要在仪表板中查看的数据。

这是HTML:

<div class="card-header">
    <div class="item-input">
        <select id="balances">
            <option value="tb">Trading Balance</option>
            <option value="mt">Managed Trading</option>
            <option value="mm">Managed Mining</option>
        </select>
    </div>
    <span class="traders">Traders - <b class="gg">3000</b></span>
</div>

这是jQuery:

var email = window.localStorage.getItem("email");
var url = 'http://example.world/mobile/index.php/welcome/trading_balance';
$('#balances').on('change', function() {
  var tbalances = this.value;

  if(tbalances === 'tb'){
  var url = 'http://example.world/mobile/index.php/welcome/trading_balance';
  }
  if(tbalances === 'mt'){
  var url = 'http://example.world/mobile/index.php/welcome/managed_trading_balance';
  }
  if(tbalances === 'mm'){
  var url = 'http://example.world/mobile/index.php/welcome/managed_mining_balance';
  }
});
//Get Trading Balance
setInterval(function() {
$.ajax({
        url: url,
        type: 'POST',
        data: email,
        crossOrigin: true,
        async: true,
        success: function (data) {
        $('.jbalance').html(data);
        },
        error: function (data, textStatus, jqXHR) { 
        if(typeof data === 'object'){
        var data = 'No Internet Connection Detected';
        }    
        },
        cache: false,
        contentType: false,
        processData: false
    });
}, 5 * 1000);

在选择时,url变量仍然保留:

var url = 'http://example.world/mobile/index.php/welcome/trading_balance';

我试图在jQuery ajax发布请求中使用所选的选项,但这种情况并没有发生。

如何在jquery ajax post请求中使用所选的URL?

2 个答案:

答案 0 :(得分:0)

因为你每次都使用var url重新创建另一个名为url的局部变量,所以外部变量url不会改变,并使用if else-if而不是多个if,这是一个解决方案:

if(tbalances === 'tb'){
  // no var, just change the already created variable url
  url = 'http://example.world/mobile/index.php/welcome/trading_balance';
}
else if(tbalances === 'mt'){
  // no var, just change the already created variable url
  url = 'http://example.world/mobile/index.php/welcome/managed_trading_balance';
}
else if(tbalances === 'mm'){
  // no var, just change the already created variable url
  url = 'http://example.world/mobile/index.php/welcome/managed_mining_balance';
}

答案 1 :(得分:0)

试试这个:

var url = 'http://example.world/mobile/index.php/welcome/trading_balance';
$('#balances').on('change', function() {
	var tbalances = $('#balances').val(); //**change

	if(tbalances.indexOf("tb") >= 0){  //**change
		var url = 'http://example.world/mobile/index.php/welcome/trading_balance';
		alert(url);
	}
	if(tbalances.indexOf("mt") >= 0){ //**change
		var url = 'http://example.world/mobile/index.php/welcome/managed_trading_balance';
		alert(url);
	}
	if(tbalances.indexOf("mm") >= 0){ //**change
		var url = 'http://example.world/mobile/index.php/welcome/managed_mining_balance';
		alert(url);
	}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="card-header">
    <div class="item-input">
        <select id="balances">
        	<option value="">-- Choose an Option --</option>
            <option value="tb">Trading Balance</option>
            <option value="mt">Managed Trading</option>
            <option value="mm">Managed Mining</option>
        </select>
    </div><span class="traders">Traders - <b class="gg">3000</b></span>
</div>