Javascript 从输入(收音机)中获取值并打印出来。。。使用jQuery
HTML代码Javascript 从输入(收音机)中获取值并打印出来。。。使用jQuery,javascript,jquery,html,Javascript,Jquery,Html,HTML代码 <script language="javascript" type="text/javascript"> $('#leftDiv').ready(function(){ $("form").submit(function () { //data structure var radios = []; //for printing purposes
<script language="javascript" type="text/javascript">
$('#leftDiv').ready(function(){
$("form").submit(function () {
//data structure
var radios = [];
//for printing purposes
var arrayValues = $("#arrayVal");
//Getting the values
$("form :radio:checked").each(function() {
radios = $(this).attr('value');
});
//Printing the values
$.each(radios, function( index, value ){
arrayValues.append(
$(value)
);
});
})
});
</script>
不知道为什么,但只能从无线电组获取最后一个值。
我做错了什么?而不是
<html>
<body>
<div id="leftDiv" class="container">
<form action="" method="get">
<fieldset>
<p>Something 0</p>
<lable>Yes</label>
<input type="radio" name="Group0" value="1" >
<lable>No</label>
<input type="radio" name="Group0" value="0" >
</fieldset>
<fieldset>
<p>Something 1</p>
<lable>Yes</label>
<input type="radio" name="Group1" value="1" >
<lable>No</label>
<input type="radio" name="Group1" value="0" >
</fieldset>
<fieldset>
<p>Something 2</p>
<lable>Yes</label>
<input type="radio" name="Group2" value="1" >
<lable>No</label>
<input type="radio" name="Group2" value="0" >
</fieldset>
<fieldset>
<input type="submit" name="update" class="button" value="Submit">
</fieldset>
</form>
</div>
</body>
</html>
试一试
而不是
<html>
<body>
<div id="leftDiv" class="container">
<form action="" method="get">
<fieldset>
<p>Something 0</p>
<lable>Yes</label>
<input type="radio" name="Group0" value="1" >
<lable>No</label>
<input type="radio" name="Group0" value="0" >
</fieldset>
<fieldset>
<p>Something 1</p>
<lable>Yes</label>
<input type="radio" name="Group1" value="1" >
<lable>No</label>
<input type="radio" name="Group1" value="0" >
</fieldset>
<fieldset>
<p>Something 2</p>
<lable>Yes</label>
<input type="radio" name="Group2" value="1" >
<lable>No</label>
<input type="radio" name="Group2" value="0" >
</fieldset>
<fieldset>
<input type="submit" name="update" class="button" value="Submit">
</fieldset>
</form>
</div>
</body>
</html>
试一试
当你得到这些值时,你有一行JS:
radios.push($(this).attr('value'));
你肯定是指收音机[收音机.长度]?这有帮助吗?当你得到这些值时,你有一行JS:
radios.push($(this).attr('value'));
radios = $(this).attr('value');
你肯定是指收音机[收音机.长度]?这有帮助吗
radios = $(this).attr('value');
通过Each的每次迭代都会覆盖收音机中的值。这就是为什么一旦循环存在,就只有上一次迭代的值
通过Each的每次迭代都会覆盖收音机中的值。这就是为什么一旦循环存在,就只有上一次迭代的值。我认为这应该是:
$("form :radio:checked").each(function() {
radios = $(this).attr('value');
});
我认为这应该是:
$("form :radio:checked").each(function() {
radios = $(this).attr('value');
});
获取值而不是属性
获取值,而不是属性。我认为每次以$form:radio:checked形式运行时,您都会覆盖radio的值,而不是将其添加到函数中 你有这个:
$("form :radio:checked").each(function(i) {
radios[i] = $(this).val();
});
…而不是这个:
// Getting the values
$("form :radio:checked").each(function() {
radios = $(this).attr('value');
});
…因此,当循环结束时,只剩下最终值。我认为每次以$form:radio:checked进行循环时,每个函数都会覆盖收音机的值,而不是添加到它 你有这个:
$("form :radio:checked").each(function(i) {
radios[i] = $(this).val();
});
…而不是这个:
// Getting the values
$("form :radio:checked").each(function() {
radios = $(this).attr('value');
});
…因此,当循环结束时,只剩下最终值。lol,好的,edoloughlin就像一个符咒一样工作,我发现这也可以工作,但不知道是否有任何区别:…functionindex radios[index]=$this.attr'value';哈哈,好吧,edoloughlin就像一个魔咒,我发现这也可以,但不知道有什么区别:…functionindex radios[index]=$this.attr'value';我只是想澄清一下,attrvalue是恶魔,也是许多bug的源头。永远不要使用它。只是为了澄清。attrvalue是恶魔,也是许多bug的来源。永远不要使用它。