繁体   English   中英

使用JavaScript启用/禁用按钮的问题

[英]problem with enable/disable button using javascript

我正在尝试添加一个复选框,以启用/禁用编辑按钮。 我正在检索价目表并将其显示在表格内。 当我将javascript添加到php代码时,它不起作用。 下面是我的代码

<table border="1">
  <tr>

    <td width="100">Fee % </td>
    <td width="100">Price</td>
    <td width="100">Total</td>
    <td width="102">&nbsp;</td>

  </tr>
  <tr>
    <?php
  $sql1="select * from pricelist";
  $result1=mysql_query($sql1) or die(mysql_error());

  while ($row=mysql_fetch_array($result1)) {

$id=$row['id'];
$price=$row['h_price'];
$a=0;

print "<form id='form1' name='$a+' method='post' action=''>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>"; 
print "</tr>";
print "</form>";

    }
   ?>
</table>

有人可以告诉我我在做什么错吗?

谢谢

我认为$a+会导致语法错误。 您需要像$a++这样在循环结束时增加$a 另请参阅页面源,并查看来自服务器的内容。

我建议对html + php输出使用以下格式

<form id='form1' name='<?=$a++?>' method='post' action=''>
<tr>
<td><input name='fees' value ='<?=$fees?>' type='text' size='4' /></td>
<td><input name='price' value ='<?=$price?>' type='text' size='15' /></td>
<td><input type='checkbox' onclick='this.disabled = !this.checked;'><td>
<td><input type='submit' name='<?=$a++?>' value='Submit' disabled='disabled' /></td>
</tr>
</form>
<?

您还需要使<tr>进入while循环。

从循环中取出以下代码...它将创建多个表单标签...

打印“ <form id='form1' name='$a+' method='post' action=''> ”;

将“ <tr></tr> ”放入循环中...

最终代码如下:

<form id='form1' name='fm1' method='post' action=''>  <tr>
    <?php
  $sql1="select * from pricelist";
  $result1=mysql_query($sql1) or die(mysql_error());

  while ($row=mysql_fetch_array($result1)) {

$id=$row['id'];
$price=$row['h_price'];
$a=0;
print "<tr>";
print "<td><input name='fees' value ='$fees' type='text' size='4' /></td>";
print "<td><input name='price' value ='$price' type='text' size='15' /></td>";
echo "<td><input type='checkbox' onclick='this.$a+.disabled = !this.checked;'><td>";
print"<td><input type='submit' name='$a+' value='Submit' disabled='disabled' /></td>"; 
print "</tr>";

    }
print "</form>";
   ?>

接下来,如果您只想控制按钮,则

<input type='checkbox' onclick='document.$a.submit.disabled = !this.checked;' />

并确保以下内容:1.)表单名称应为$ a,即<form name='$a' ...>

2.)提交按钮的名称应为提交,即<input type='submit' name='submit'...>

3.)仅在循环结束时才增加$ a变量,即

$a++;
}

在您设置公式的html部分中,您必须使用“”来转义php代码。 例:

print "<form id='form1' name='".$a+."' method='post' action=''>";

但是正如Adeel已经说过的那样,$ a +是多少?

而且您必须以这种方式编辑所有打印件和回显,即从HTML源代码中排除了php表达式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM