【发布时间】:2021-10-17 14:06:37
【问题描述】:
我正在努力获得一个带有 3 个按钮的简单表单,以将它们的值添加到变量中并将结果输出到文本框中。我已经尝试了几件事,但没有运气,请如果有人能告诉我我做错了什么。
<body>
$curr_price = 4000;
<br>
<hr/><form>
<input type="text" name="result_test" id="result-c" value="">
<input type="reset" value="Reset">
<br>
<input class="btn" type="button" name="value1" value=" 500" id="btnone" title="your-tooltip-here">
<input class="btn" type="button" name="value1" value="1000" id="BtnTwo">
<input class="btn" type="button" name="value2" value="1500" id="BtnThree">
</form>
</body>
<script>
$('.btn').on('click', function(e) {
e.preventDefault();
// Get val of btn
let a = parseInt( $( this ).val() );
// Get val of form imput
var b = parseInt('<?=$curr_price; ?>');
// Sanity check on b to make sure we are working with a number
if ( isNaN( b ) )
b = 0;
// Do the addition and place value in form input
$( '#result-c' ).val( a + b );
});
</script
【问题讨论】:
-
$curr_price = 4000;?为什么这没有包含在 PHP 标签中?另外,为什么你需要在 PHP 值的前端代码中进行“健全性检查”? -
嗨@RokoC.Buljan。我也尝试将它包装在 PHP 标记中,结果相同。如果我删除健全性检查,它会在文本字段中返回带有变量名称的按钮的值。例如 1000$curr_price。我只是不知道如何获取 jQuery 脚本来获取变量的值。你有什么建议吗。
-
按钮没有值。为此使用输入文本/数字
标签: jquery forms calculation