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