jquery中按钮的onclick函数不起作用 [英] onclick function for button in jquery is not working
问题描述
我是jquery的新手,我对onclick函数有一个愚蠢的问题。请帮帮我。
我有一个表,当查看器单击添加新按钮时,我将使用jquery向现有表添加一行。在新行中,有2个文本框用于输入数据,还有一个按钮用于将数据从文本框传输到php文件。
这是我的代码:
html代码:
I am a newbie in jquery, I have a stupid question for onclick function. Please help me.
I have a table, when viewer click "Add new" button, I will using jquery add a row to existing table. In new row, there are 2 textbox to input data and a button to transfer data from textbox to php file.
This is my code:
html code:
<INPUT type="button" value="Add Row" onclick="addRow('dataTable')" />
<INPUT type="button" value="Delete Row" onclick="deleteRow('dataTable')" />
<TABLE id="dataTable" width="350px" border="1">
<thead>
<td>No</td>
<td>First name</td>
<td>Last name</td>
</thead>
<?php
include("functions/connect_db.php");
$no=0;
$sql="SELECT * FROM `fullnames`";
$result=mysqli_query($conn,$sql) or die('Could not select Machine'.mysqli_error($conn));
while ($set=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$no++;
$first=$set['firstname'];
$last=$set['lastname'];
?>
<tbody>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $first; ?></td>
<td><?php echo $last; ?></td>
</tr>
<?php } ?>
</tbody>
</TABLE>
</pre>
script:
<pre><pre lang="text">
<script>
function addRow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell1 = row.insertCell(0);
cell1.innerHTML = rowCount ;
var cell2 = row.insertCell(1);
var element1 = document.createElement("input");
element1.type = "text";
element1.id="first_input_"+rowCount;
cell2.appendChild(element1);
var cell3 = row.insertCell(2);
var element2 = document.createElement("input");
element2.type = "text";
element2.id="last_input_"+rowCount;
cell3.appendChild(element2);
var cell4=row.insertCell(3);
var element3 = document.createElement('input');
element3.setAttribute('type','button');
element3.id="#button_"+rowCount;
element3.className="butt_class";
element3.value='Save';
cell4.appendChild(element3);
}
$(document).ready(function() {
$(".butt_class").click(function()
{
var ID=$(this).attr('id');
var first=$("#first_input_"+ID).val();
var last=$("#last_input_"+ID).val();
if(first.length<0 && last.length>0)
{
$.ajax({
type: "POST",
url: "add_new.php",
data: dataString,
cache: false
});
}
})
})
<script>
当我点击每行中的保存按钮时,什么都不会发生。我尝试通过替换所有单击保存功能,但没有更改。
请帮助我。
P / S:很抱歉因为我没有描述此错误代码。
提前谢谢!
When I click to Save button in each row, nothing is happen. I tried by replace all click Save function with alert but there is not change.
Pls help me.
P/S: Sorry because I didn't describe error for this code.
Thank you in advance!
推荐答案
no = 0;
no=0;
sql = SELECT * FROM`fullnames` ;
sql="SELECT * FROM `fullnames`";
result = mysqli_query(
result=mysqli_query(
这篇关于jquery中按钮的onclick函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!