当下拉框中的值来自 mysql 时,使用 Javascript 禁用文本字段
我有一个简单的 HTML 脚本,使用下拉菜单。当选择值 1 时,用户可以在文本字段中写入,如果选择值 2,则禁用文本字段。
但是,我更改了下拉菜单的值,因此一个值来自 mysql 表(使用 PHP),另一个值仍然是“选项值=”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>
I have a simple script in HTML, using a dropdown menu. When the value 1 is selected, the user can write in the text field, if value 2 is selected, it disables the text field.
However, i changed the values of the dropdown menu, so that one value was from a mysql table(using PHP) and the other remained 'option value='1''. Yet now neither text field is disabled. Below is the code.
`<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
}
}
`
And the PHP section
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>
I have tried taking the second option value out of the loop, putting it into html, editing the variable in the javascript function. There is not a fault with the PHP as it is retrieving the right results and displaying it, yet the text field doesnt become disabled.
Does anyone know of a possible solution?
Thanks
Changes made to test simple php variable
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少 HTML 中的
echo
以及 javascript 中echo
周围的一些引号。另外,
$wah
在初始化之前使用。You are missing an
echo
in the HTML and some quotes around theecho
in the javascript.Also,
$wah
is used before initialized.试试这个:
Try this out: