使用jQuery将值传递给结果
我有一个简单的表格:使用jQuery将值传递给结果,jquery,forms-authentication,Jquery,Forms Authentication,我有一个简单的表格: <form id="radio_form"> <fieldset> <label><input type="radio" name="color" value="1" />Red</label><br /> <label><input type="radio" name="color" value="2" />Yellow</label><b
<form id="radio_form">
<fieldset>
<label><input type="radio" name="color" value="1" />Red</label><br />
<label><input type="radio" name="color" value="2" />Yellow</label><br />
<label><input type="radio" name="color" value="3" />Blue</label><br />
<label><input type="radio" name="color" value="4" />Purple</label><br />
</fieldset>
<fieldset>
<label><input type="radio" name="time" value="6" />12</label><br />
<label><input type="radio" name="time" value="7" />11</label><br />
<label><input type="radio" name="time" value="8" />10</label><br />
<label><input type="radio" name="time" value="9" />9</label><br />
</fieldset>
</form>
非常感谢您的帮助
与大多数jQuery的getter重载一样,
val()
只返回第一项的值。您可以使用map()并加入结果数组:
$(function() {
var value = $('input:radio:checked').map(function() {
return this.value;
}).get().join(" "); // You could join with a comma if you like
$('.result').text(value); // Use text() unless you're adding HTML
});
啊,看来你在追求一个跑步总数。在这种情况下,您将需要使用each():
上面代码中的
+this.value
将值强制为一个数字。与大多数jQuery的getter重载一样,val()
只返回第一项的值。您可以使用map()并加入结果数组:
$(function() {
var value = $('input:radio:checked').map(function() {
return this.value;
}).get().join(" "); // You could join with a comma if you like
$('.result').text(value); // Use text() unless you're adding HTML
});
啊,看来你在追求一个跑步总数。在这种情况下,您将需要使用each():
+上面代码中的this.value
将值强制为一个数字。效果很好,唯一的问题是我没有指定结果是这些值的总和。很抱歉我怎么能这样呢?@Dom:我更新了我的答案,也给了你一个解决方案。这很有效,唯一的问题是我没有指定我想要这些值的总和作为结果。很抱歉我怎么能这样呢?@Dom:我更新了我的答案,也给了你一个解决方案。
$(function() {
var value = $('input:radio:checked').map(function() {
return this.value;
}).get().join(" "); // You could join with a comma if you like
$('.result').text(value); // Use text() unless you're adding HTML
});
$(function() {
var value = 0;
$('input:radio:checked').each(function() {
value += +this.value;
});
$('.result').text(value); // Use text() unless you're adding HTML
});