所以我正在尝试
$(function() {
convertPrice = function() {
var myAnswer, myVal, myRate;
myVal = $('#slider1').val();
myRate = $('#rate').val();
myVal = parseFloat(myVal); // convert string type
myAnswer = myVal * myRate;
$('#Answer').val(myAnswer);
}
$("#convertButton").click(convertPrice);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div data-role="fieldcontain">
<label for="R">Cost Per Person:</label>
<input type="text" value="$990" name="rate" id="rate" readonly />
</div>
<label for="slider">Number of People Going?:</label>
<input type="range" name="slider" id="slider1" value="1" min="1" max="30" />
</div>
<input type="button" value="Convert" id="convertButton" />
<div data-role="fieldcontain">
<label for="theAnswer">Results:</label>
<input type="text" name="textInput" id="Answer" value="" readonly />
</div>
&#13;
所以当我点击按钮时,它只是以NaN的形式返回?有什么建议吗?
答案 0 :(得分:0)
对于费率输入框,您已指定$ in value标记。所以它给出了NaN值。将该行代码更改为
$
然后它肯定会起作用
答案 1 :(得分:0)
$(function() {
convertPrice = function() {
var myAnswer, myVal, myRate;
myVal = $('#slider1').val();
myRate = $('#rate').val();
myRate = myRate.replace("$","");
myVal = parseFloat(myVal); // convert string type
myAnswer = myVal * myRate;
$('#Answer').val(myAnswer);
}
$("#convertButton").click(convertPrice);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<div data-role="fieldcontain">
<label for="R">Cost Per Person:</label>
<input type="text" value="$990" name="rate" id="rate" readonly />
</div>
<label for="slider">Number of People Going?:</label>
<input type="range" name="slider" id="slider1" value="1" min="1" max="30" />
</div>
<input type="button" value="Convert" id="convertButton" />
<div data-role="fieldcontain">
<label for="theAnswer">Results:</label>
<input type="text" name="textInput" id="Answer" value="" readonly />
</div>
&#13;
答案 2 :(得分:0)
如前面的回答所述,$ sign是问题。你可以删除那个符号它会起作用。如果你想保留那个符号然后使用regex
你可以在做计算时删除那个符号< / p>
var myAnswer, myVal, myRate;
myVal = $('#slider1').val();
myRate = $('#rate').val();
myVal = parseFloat(myVal); // convert string type
myRate = parseFloat(myRate.replace(/[^0-9\.-]+/g, "")); // here before convserting we need to use regex to replace $ with ""
myAnswer = myVal * myRate;
$('#Answer').val(myAnswer);