我正在尝试将输入值发送到另一个页面。
$( this ).attr( "href", '?module=module_progress_report&Subject='+ $('input[name=subject]:checked').val()+ '&Centre_Selected_ID='+ encodeURIComponent($( this ).attr('data-centre')) + '&Class_Selected_Year='+ encodeURIComponent($( this ).attr('data-year')) + '&Class_Selected_All='+ encodeURIComponent($( this ).attr('data-all-centre')) +'&StartDate='+$('#report_date_start').val()+'&EndDate=18/12/2016');
下面,
$('input[name=subject]:checked').val()
这总是返回undefined。该值未被选中。如果我选择其中一个选项作为默认值,则会存储其值。我需要做什么?
这是PHP和HTML:
<form class="small-box-footer" style="text-align:left;padding:10px;" method="post" name="nameHere">
<?php
$query = "SELECT * FROM subject";
//$query="SELECT * FROM $base_name";
$result = mysql_query($query);
while ($row39 = mysql_fetch_array($result)) {
$Referrer_ID = $row39['Subject_ID'];
$Referrer_Name = $row39['Subject_Name'];
?>
<input type="checkbox" class="subject-selected" name="subject" value="<?=$Referrer_ID?>"> <?=$Referrer_Name?><?=$Referrer_ID?><br />
<?php } ?>
</form>
以下是HTML:
<input type="checkbox" class="subject-selected" name="subject" value="2"> GCSE Maths2<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="GCSE English" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="3"> GCSE English3<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="GCSE Science" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="4"> GCSE Science4<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="GCSE Art" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="5"> GCSE Art5<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="GCSE Sociology" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="6"> GCSE Sociology6<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="OCR Nationals ICT" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="8"> OCR Nationals ICT8<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="OCR Nationals Sports" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="9"> OCR Nationals Sports9<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="OCR Nationals Business Studies" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="10"> OCR Nationals Business Studies10<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="Entry Science" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="11"> Entry Science11<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="Functional Skills English" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="12"> Functional Skills English12<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="Functional Skills Maths" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="13"> Functional Skills Maths13<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="ESOL" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="14"> ESOL14<br />
<!--<a href="#" class="small-box-footer" data-id="<?php=$Referrer_Name?>" data-subject="Preparation for Working Life" data-report-range="month">
<i class="fa fa-arrow-circle-right"></i>
</a>
-->
<input type="checkbox" class="subject-selected" name="subject" value="15"> Preparation for Working Life15<br />
答案 0 :(得分:1)
尝试在控制台上运行相同的代码$('input[name=subject]:checked').val()
并查看它给出的内容。
for :checked
#确保您输入的元素类型为checkbox
您也可以使用:
$('input[name="input_name"]').is('checked'); // will return true/false
答案 1 :(得分:0)
您需要将checked
替换为selected
:
$('input[name=subject]:selected').val()
答案 2 :(得分:-1)
Syntax: $( "input[value='name']" )
try this
$('input[name="subject"]:checked').val()