当下拉框中的值来自 mysql 时,使用 Javascript 禁用文本字段

发布于 2024-09-01 03:31:10 字数 1925 浏览 1 评论 0原文

我有一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-09-08 03:31:10

您缺少 HTML 中的 echo 以及 javascript 中 echo 周围的一些引号。

另外,$wah 在初始化之前使用。

You are missing an echo in the HTML and some quotes around the echo in the javascript.

Also, $wah is used before initialized.

荭秂 2024-09-08 03:31:10

试试这个:

<?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> 

Try this out:

<?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> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文