我有两个单选按钮,它们都有值。当选择其中一个并单击按钮/链接时,需要将所选单选按钮的值输出到文本框。
这是我的 HTML:
<form class="discount-choice" method="post" action="#">
<ul>
<li>
<input type="radio" value="1" name="discount" id="1-month" class="discount-checkbox" />
<label for="1-month">1 Month - no discount</label>
</li>
<li>
<input type="radio" value="0.95" name="discount" id="4-months" class="discount-checkbox" />
<label for="4-months">4 months - 5% discount</label>
</li>
</ul>
<a href="#" class="get-result">Get Result</a>
<input type="text" class="result" />
</form>
和我的 JavaScript (jQuery):
var discount = $(".discount-checkbox").on("click", function () {
return this.value;
});
$(".get-result").on("click", function() {
$(".result").attr("value", discount);
});
当我单击按钮(选择单选按钮后)时,我只会在文本框中显示 [object Object]
。
我不确定将变量设置为 jQuery 函数的语法,因此这可能是我出错的地方。如果有人能给我一些指导,那就太好了。
感谢您的帮助:)
最佳答案
discount
设置为 $(".discount-checkbox")
返回的 jQuery 对象。您需要绑定(bind)一个更新变量的点击处理程序:
var discount = 0;
$(".discount-checkbox").on("click", function () {
discount = this.value;
});
关于javascript - 在 .click() 函数中获取 .change() 函数的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25119704/