简体   繁体   English

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

[英]problem with enable/disable button using javascript

Im am trying to add a check box that will enable/disable the edit button. 我正在尝试添加一个复选框,以启用/禁用编辑按钮。 Im retrieving a price list and displaying it inside a table. 我正在检索价目表并将其显示在表格内。 When i add the javascript into the php code it doesn't work. 当我将javascript添加到php代码时,它不起作用。 Below is my code 下面是我的代码

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

Can someone please tell me what am i doing wrong? 有人可以告诉我我在做什么错吗?

Thanks 谢谢

i think $a+ will cause syntax error. 我认为$a+会导致语法错误。 you need to increment $a at the end of loop like $a++ . 您需要像$a++这样在循环结束时增加$a Also see page source and see what is coming from server. 另请参阅页面源,并查看来自服务器的内容。

I would recommend using following format for html + php output 我建议对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>
<?

you also need to have < tr > to be in the while loop. 您还需要使<tr>进入while循环。

take out the following code out of loop... it creates multiple form tag... 从循环中取出以下代码...它将创建多个表单标签...

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

Place " <tr></tr> " inside the loop... 将“ <tr></tr> ”放入循环中...

final code looks like: 最终代码如下:

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

Next if u want to control the button only then 接下来,如果您只想控制按钮,则

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

and make sure of the following things: 1.) form name should be $a ie <form name='$a' ...> 并确保以下内容:1.)表单名称应为$ a,即<form name='$a' ...>

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

3.) increase the $a variable only at the end of loop ie 3.)仅在循环结束时才增加$ a变量,即

$a++;
}

In your html part where you set up the formular you have to use " " to escape the php code. 在您设置公式的html部分中,您必须使用“”来转义php代码。 example: 例:

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

But as Adeel said already, what is $a+ ? 但是正如Adeel已经说过的那样,$ a +是多少?

And you have to edit all prints and echoes in the way, that php expressions are excaped from the HTML source. 而且您必须以这种方式编辑所有打印件和回显,即从HTML源代码中排除了php表达式。

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

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