在php while循环中传递javascript操作
我需要以下功能: “选中复选框时,相应的文本框必须高亮显示,用户可以在其中输入一些文本…” 复选框正在工作,但当选中复选框时,相应的文本框未激活,如何解决此问题 以下是我的php代码:在php while循环中传递javascript操作,php,javascript,Php,Javascript,我需要以下功能: “选中复选框时,相应的文本框必须高亮显示,用户可以在其中输入一些文本…” 复选框正在工作,但当选中复选框时,相应的文本框未激活,如何解决此问题 以下是我的php代码: <?php if ($handle = opendir('/year_no/albums')) { $blacklist = array('.', '..'); while (false !== ($file = readdir($handle))) { if (!in_ar
<?php
if ($handle = opendir('/year_no/albums')) {
$blacklist = array('.', '..');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "<form name='form1'>
<div class='controlset-pad'>
<font color='black'><label><input type='checkbox' name='album[]' value='$album' onclick='enable_text(this.checked)' class='medium'/><span>$album</span></label><br/></div>";
echo"<script>
function enable_text(status)
{
status=!status;
document.form1.other_name.disabled = status;
}
</script>";
echo "<div class='field4'><label>Add your comment here</label><input type='text' name='other_name' class='medium' disabled='disabled'/></div></form>";
}
}
closedir($handle);
}
?>
您的代码非常完美,只需添加一个小改动,如下所示
而不是id
document.getElementById('other_name').disabled = status;
请注意,id
对于每个元素都必须是唯一的
编辑并使用此代码
<?php
if ($handle = opendir('/year_no/albums')) {
$blacklist = array('.', '..');
while (false !== ($file = readdir($handle))) {
if (!in_array($file, $blacklist)) {
echo "<form name='form1'>
<div class='controlset-pad'>
<font color='black'><label><input type='checkbox' name='album[]' value='$album' onclick='enable_text(this.checked, $album)' class='medium'/><span>$album</span></label><br/>
</div>?";
echo"<script>
function enable_text(status, album)
{
status=!status;
document.getElementById(album).disabled = status;
}
</script>";
echo "<div class='field4'><label>Add your comment here</label><input type='text' name='other_name' id = '$album' class='medium' disabled='disabled'/></div></form>";
}
}
closedir($handle);
}
?>
使用此示例
<form name="form1">
<script>
function enable_text(status)
{
alert(status)
status=!status;
document.form1.other_name.disabled = status;
}
</script>
<input type='checkbox' name='album' value='hi' onclick='enable_text(this.checked)' class='medium'/>
<input type='text' name='other_name' class='medium' disabled='disabled'/>
</form>
功能启用\u文本(状态)
{
警报(状态)
状态=!状态;
document.form1.other_name.disabled=状态;
}
试试这个:
正如您所说,下面的代码具有在选中复选框时启用文本框的功能,并在未选中时再次禁用它
这是一张工作票
$(文档).ready(函数(e){
$(“输入[type=checkbox]”)。在(“单击”,函数(){
如果($(this).is(':checked')){
//如果选中复选框,请在此处执行任何您想执行的操作。
$(this.attr(“id”).removeAttr(“禁用”);
$(“#drpdwn”+$(this.attr(“id”)).removeAttr(“禁用”);
}否则{
//如果不检查。
$(this.attr(“id”)).attr(“disabled”、“disabled”);
$(“#drpdwn”+$(this.attr(“id”)).attr(“disabled”、“disabled”);
}
});
});
使用或退出PHP模式可输出大量HTML。font
标记长期以来一直被弃用。请改用CSS。请确保使用类似于防止意外(或恶意)XSS的函数来转义可能不安全的字符串,如$album
。为什么要将函数打印n次?您必须将Javascript代码放在页面的页眉或页脚中。将类/ID分配给while循环中的复选框/文本字段,并编写JS代码来使用这些ID/类进行操作。您的代码也在我的系统上运行感谢您的回复,您能在代码中告诉我在哪里添加吗?内部函数enable_text()是的,我已经修改过,但当选中复选框时,同样的问题,文本框未激活。@ManishGoyal-他正在while循环中重复函数enable_text()
。不能使用相同的名称定义函数,因为它不起作用。而循环正在用相同的名称定义相同的函数。@highlander141-您对JQuery感到满意吗?@highlander141-很高兴!NJoi!!:)兄弟,我需要更多的信息,我可以得到你的邮箱吗?再次感谢,我真正需要的是,复选框与目录列表相关,直到目录相册,稍后当选中一个复选框时,在相关的文本框中,必须出现一个下拉菜单,其中列出了所选复选框的所有子目录,你可以引导我吗?我不明白你的意思。您所说的是什么意思?在关联的文本框中,必须出现一个下拉菜单
?选中复选框时,我们必须激活一个下拉列表,其中包含所选复选框的子目录。。
<form name="form1">
<script>
function enable_text(status)
{
alert(status)
status=!status;
document.form1.other_name.disabled = status;
}
</script>
<input type='checkbox' name='album' value='hi' onclick='enable_text(this.checked)' class='medium'/>
<input type='text' name='other_name' class='medium' disabled='disabled'/>
</form>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"> </script>
<!-- include JQuery using above line -->
<!-- Here is your small javascript -->
<script type="text/javascript">
$(document).ready(function(e) {
$("input[type=checkbox]").on("click",function(){
if($(this).is(':checked')){
//If checkbox is checked do whatever you want to do here.
$("#txt_"+$(this).attr("id")).removeAttr("disabled");
$("#drpdwn_"+$(this).attr("id")).removeAttr("disabled");
} else {
// If unchecked.
$("#txt_"+$(this).attr("id")).attr("disabled","disabled");
$("#drpdwn_"+$(this).attr("id")).attr("disabled","disabled");
}
});
});
</script>
<?php
$dir = "/year_no/albums";
if(is_dir($dir)) {
if ($dh = opendir($dir)) {
$blacklist = array('.', '..');
$count=1;
while (($file = readdir($dh)) !== false) {
if (!in_array($file, $blacklist)){
echo "<div class='controlset-pad'>
<font color='black'>
<label>
<input type='checkbox' name='file[]' id='".$count."' value='enabletxtbox' class='medium'/>
<span>".$file."</span>
</label>
</div>";
echo "<div class='field4'>
<label>Add your comment here</label>
<input type='text' name='sub' id='txt_".$count."' class='medium' disabled='disabled'/>";
if(filetype($dir.$file) == "dir"){
$dir2 = $dir.$file;
$dh2=opendir($dir2);
$drpdwn="<select id='drpdwn_".$count."' disabled='disabled'>";
while(($file2 = readdir($dh2)) !== false){
if(!in_array($file2, $blacklist)){
$drpdwn.='<option>'.$file2.'</option>';
}
}
$drpdwn.="</select>";
closedir($dh2);
echo $drpdwn;
}
echo"</div>";
}
$count++;
}
closedir($dh);
}
}
?>