使用jQuery设置元素,我错过了什么。

时间:2017-09-28 01:39:59

标签: javascript jquery

const form = document.getElementById("form");
let accountBalance = $("#accountBalance");
let stockPrice = $("#stockPrice");
accountBalance = parseInt(accountBalance);
stockPrice = parseFloat(stockPrice);


// const div = document.getElementById('div');

$("#display").hide();

let amountPerTrade = function amountPerTrade() {
    return accountBalance / 4;
}

// Returns shares that are allowed per trade
let sharesPerTrade = function sharesPerTrade() {
    const amountPerTrade = accountBalance / 4;
    return Math.floor(amountPerTrade / stockPrice);
}

// Returns amount that should be calculated for limit and stop amounts
function getExitAmount(percentage) {
    let amount = ((accountBalance * percentage) / sharesPerTrade()).toFixed(2);
    return amount;
}



let limitPrice = function limitPrice() {
    return getExitAmount(.03) + stockPrice;
}

let stopPrice = function stopPrice() {
    return stockPrice - getExitAmount(.01);
}

let possibleProfit = function possibleProfit() {
    return (getExitAmount(.03) * sharesPerTrade()).toFixed(2);
}

let possibleLoss = function possibleLoss() {
    return (getExitAmount(.01) * sharesPerTrade()).toFixed(2);
}



$("form").submit(function () {
    $("form").hide();

    $("#amountPerTrade").html(amountPerTrade);
    $("#sharesPerTrade").html(sharesPerTrade);
    $("#limitPrice").html(limitPrice);
    $("#stopPrice").html(stopPrice);
    $("#possibleProfit").html(possibleProfit);
    $("#possibleLoss").html(possibleLoss);


    $("#display").show();
    return false;
});

$("#reset").click(function (){
  $("form").show();

  $("#display").hide();
  return false;
});

所以这是我的js代码,我在HTML中的所有id都对应。我已经检查过再次检查并再次检查。我知道我在这里错过了一些愚蠢的东西。但每次我运行它时,要么没有任何内容放入HTML元素或我NaN。 完整代码位于https://github.com/rustycomer/stock-strategy-calculator/tree/Update2.0

2 个答案:

答案 0 :(得分:0)

您的代码中存在许多问题。
我已对其进行了一些更改,因此现在可以显示结果。你可以先检查一下。

const form = document.getElementById("form");
let accountBalance = $("#accountBalance")[0].value;
let stockPrice = $("#stockPrice")[0].value;



// const div = document.getElementById('div');

$("#display").hide();

let amountPerTrade = function amountPerTrade() {
    return accountBalance / 4;
}

// Returns shares that are allowed per trade
let sharesPerTrade = function sharesPerTrade() {
    let amountPerTrade = accountBalance / 4;
    return Math.floor(amountPerTrade / stockPrice);
}

// Returns amount that should be calculated for limit and stop amounts
function getExitAmount(percentage) {
    let amount = (((accountBalance) * percentage) / sharesPerTrade()).toFixed(2);
    return amount;
}



let limitPrice = function() {
    return getExitAmount(.03) + stockPrice;
}

let stopPrice = function() {
    return stockPrice - getExitAmount(.01);
}

let possibleProfit = function() {
    return (getExitAmount(.03) * sharesPerTrade()).toFixed(2);
}

let possibleLoss = function() {
    return (getExitAmount(.01) * sharesPerTrade()).toFixed(2);
}



$("form").submit(function () {
    $("form").hide();
    accountBalance = parseInt(accountBalance[0].value);
    stockPrice = parseInt(stockPrice[0].value);
    $("#amountPerTrade").html(amountPerTrade());
    $("#sharesPerTrade").html(sharesPerTrade());
    $("#limitPrice").html(limitPrice());
    $("#stopPrice").html(stopPrice());
    $("#possibleProfit").html(possibleProfit());
    $("#possibleLoss").html(possibleLoss());

    $("#display").show();
    return false;
});

$("#reset").click(function (){
  $("form").show();

  $("#display").hide();
  return false;
});

代码的主要问题是变量或函数没有正确操作。
$("form").submit(function() { })中,$(target).html()可以接收您要插入的html代码,但不能接收您声明的功能。您需要执行这些函数以获取插入的返回值。
此外,在第2行到第3行中,您必须从输入中获取值,而不是$(target)
最后,请注意varletconst之间的差异。如果你错误地使用它们,它们会让你发疯。 祝你好运。

答案 1 :(得分:0)

不确定,但它运行了,我将代码添加到我的代码风格

const form = document.getElementById("form");
$("#display").hide();

$("form").submit(function () 
{
    $("form").hide();

	var accountBalance = $("#accountBalance").val();
	var stockPrice = $("#stockPrice").val();

	function amountPerTrade() 
	{
		return accountBalance / 4;
	}
	
	// Returns shares that are allowed per trade
	function sharesPerTrade() 
	{
		//const amountPerTrade = accountBalance / 4;
		//return Math.floor(amountPerTrade() / stockPrice);
		return (amountPerTrade() / stockPrice);
	}
	
	// Returns amount that should be calculated for limit and stop amounts
	function getExitAmount(percentage) 
	{
		return ((accountBalance * percentage) / sharesPerTrade()).toFixed(2);
	}
	
	function limitPrice() 
	{	
		return getExitAmount(0.03) + stockPrice;
	}
	
	function stopPrice() 
	{    
		return stockPrice - getExitAmount(0.01);
	}
	
	function possibleProfit() 
	{
		return (getExitAmount(0.03) * sharesPerTrade()).toFixed(2);
	}
	
	function possibleLoss() 
	{
		return (getExitAmount(0.01) * sharesPerTrade()).toFixed(2);
	}

	accountBalance = parseInt(accountBalance);
	stockPrice = parseFloat(stockPrice);


// const div = document.getElementById('div');

    $("#amountPerTrade").html(amountPerTrade());
    $("#sharesPerTrade").html(sharesPerTrade());
    $("#limitPrice").html(limitPrice());
    $("#stopPrice").html(stopPrice());
    $("#possibleProfit").html(possibleProfit());
    $("#possibleLoss").html(possibleLoss());


    $("#display").show();
    return false;
});

$("#reset").click(function (){
  $("form").show();

  $("#display").hide();
  return false;
});

enter image description here