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

时间:2010-06-07 08:56:21

标签: php 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>

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

由于

4 个答案:

答案 0 :(得分:1)

我认为$a+会导致语法错误。你需要在循环结束时增加$a,如$a++。另请参阅页面源代码,了解服务器的内容。

答案 1 :(得分:1)

我建议使用以下格式的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>
<?

你还需要&lt; tr>在while循环中。

答案 2 :(得分:1)

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

打印“<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++;
}

答案 3 :(得分:0)

在你设置公式的html部分中,你必须使用“”来逃避php代码。例如:

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

但正如阿德尔已经说过的那样,$ a +是什么?

你必须编辑所有的打印和回声,php表达式从HTML源代码中释放出来。