页面中有两个表。一个是星期五,另一个是星期六。我想从表中设置单选按钮选择的选择限制。用户不能从星期五表中的4个插槽中选择3个以上的插槽。如何为表调用javascript函数?我怎样才能做到这一点? 这是我的PHP代码和javascript函数。
这个javascript函数适用于两个表。当我从任何表格中选择3个单选按钮时,它会显示“你不能添加超过3个”。但我希望这只适用于星期五的表而不是星期六。
PHP代码:
for($time=0; $time<4; $time++){
$q.= "<td colspan='1' valign='top' width='25%'>";
$q.= "<table id='ash'>";
$q.= "<tr id='grey'>";
if($day==1){
if($time==0){
$q.= "<th width='50%'></th></tr>";
}else if($time==1){
$q.= "<th width='50%'></th></tr>";
}else if($time==2){
$q.= "<th>Reg.</th>";
$q.= "<th width='50%'>2:30 pm - 5:00 pm</th>";
$q.= "<th>Sec</th><th>Remaining</th></tr>";
}else{
$q.= "<th>Reg.</th>";
$q.= "<th width='50%'>5:00 pm - 7:30 pm</th>";
$q.= "<th>Sec</th><th>Remaining</th></tr>";
}
}
else{
$q.= "<th>Reg.</th>";
if($time==0){
$q.= "<th width='50%'>8:30 am - 11:00 am</th>";
}else if($time==1){
$q.= "<th width='50%'>11:00 am - 1:30 pm</th>";
}else if($time==2){
$q.= "<th width='50%'>2:30 pm - 5:00 pm</th>";
}else{
$q.= "<th width='50%'>5:00 pm - 7:30 pm</th>";
}
$q.= "<th>Sec</th><th>Remaining</th></tr>";
}
$query1 = "SELECT tab1.*, tab2.course_name, tab2.course_type FROM routine AS tab1, course_info AS tab2";
$result = mysql_query($query1);
if ($result && mysql_num_rows($result) > 0) {
while($row = mysql_fetch_assoc($result)){
$course = $row["course_code"];
$sec = $row["section"];
$type = $row["course_type"];
if(course_completed($std_id, $course)){
if(already_selected($course, $sec, $std_id, $year, $sem)){
$q.= "<tr bgcolor='#00FF33'><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' checked='checked' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
}
else{
$q.= "<tr bgcolor='#00FF33'><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
}
}
else{
if(already_selected($course, $sec, $std_id, $year, $sem)){
$q.= "<tr><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' checked='checked' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
}else{
$q.= "<tr><td><input type='radio' id='course_".$day."_".$time."' name='".$day.$time."' value='".$course.",".$sec.",".$type."' onclick='changeRadioValue(\"".$day."_".$time."\")' /></td>";
}
}
JavaScript函数
$("input[type='radio']").change(function(){
var count = $("input[type='radio']:checked").length;
if(count>3){
$(this).prop('checked', false);
alert("You cannot add more than 3");
}
});
答案 0 :(得分:0)
试试这个:
<?php
if(condition) {
?>
<script>//JS Code</script>
<?php
}
?>
当您加载页面时,如果条件为true,脚本标记之间的js代码将运行,您还可以链接另一个js文件而不是编写代码。
答案 1 :(得分:0)
解决了我的问题。我只是改变了我的JS功能
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
//table fri
$("#fri input[type='radio']").change(function(){
var count = $("#fri input[type='radio']:checked").length;
if(count>3){
$(this).prop('checked', false);
alert("You cannot add more than 3");
}
});
//table sat
$("#sat input[type='radio']").change(function(){
var count = $("#sat input[type='radio']:checked").length;
if(count>3){
$(this).prop('checked', false);
alert("You cannot add more than 3");
}
});
//refresh selection
$(".refresh").click(function(){
$("input[type='radio']").prop('checked', false);
});