我有一个简单的HTML脚本,使用下拉菜单。选择值1后,用户可以在文本字段中写入,如果选择值2,则禁用文本字段。
但是,我更改了下拉菜单的值,因此一个值来自mysql表(使用PHP),另一个值仍为'option value ='1''。但现在两个文本字段都没有被禁用。以下是代码。
`<script type="text/javascript">
function findselected() {
if (document.form.selmenu.value == <?php echo $id; ?>) {
document.form.txtField.disabled=true;
// return false; // not sure this line is needed
} else {
document.form.txtField.disabled=false;
// return false; // not sure this line is needed
}
} `
PHP部分
if(mysql_num_rows($SQL) == 1)
{
echo "<select name='selmenu' onChange='findselected()'>";
echo "<label>TCA_Subject</label>";
while ($row=mysql_fetch_array($SQL)) {
echo "<option value='$id'>$thing</option>";
echo "<option value='2'>Choice 2</option>";
}
}
echo "<option value=$userid>'Choice 1'</option>";
?> <option value='2'>Choice 2</option>";
</select>
我尝试将第二个选项值从循环中取出,将其放入html,在javascript函数中编辑变量。 PHP没有错,因为它正在检索正确的结果并显示它,但文本字段不会被禁用。
有没有人知道可能的解决方案?
由于
对测试简单的php变量进行的更改
<script type="text/javascript">
function findselected() {
if (document.form.selmenu.value == <?php echo $wah;?>) {
document.form.txtField.disabled=true;
// return false; // not sure this line is needed
} else {
document.form.txtField.disabled=false;
// return false; // not sure this line is needed
}
}
</script>
<?php $wah = 'hello';
echo $wah;
?>
<form action="dump.php" method="POST" name="form">
<select name="selmenu" onChange="findselected()">
<option value="1">Choice 1</option>
<option value="<?php $wah;?>">Choice 2</option>
</select>
答案 0 :(得分:0)
您错过了HTML中的echo
以及javascript中echo
周围的一些引号。
此外,$wah
在初始化之前使用。
答案 1 :(得分:0)
试试这个:
<?php $wah = 'hello';
echo $wah;
?>
<script type="text/javascript">
function findselected() {
if (document.form.selmenu.value == '<?php echo $wah;?>') {
document.form.txtField.disabled=true;
// return false; // not sure this line is needed
} else {
document.form.txtField.disabled=false;
// return false; // not sure this line is needed
}
}
</script>
<form action="dump.php" method="POST" name="form">
<select name="selmenu" onChange="findselected()">
<option value="1">Choice 1</option>
<option value="<?php echo $wah;?>">Choice 2</option>
</select>