<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form name="form1" id="form1" action="coillist" method="POST">
<table id="dataTable" border="1">
<tbody>
<tr>
<th>Coil #</th><th>Width</th><th>Gauge</th>
</tr>
<tr>
<td><input type="text" maxlength="7" size="7" name="coil_id" value="coil_11"></td>
<td><input type="text" size="7" name="width" value="120"></td>
<td><input type="text" size="5" name="gauge" value="130"></td>
</tr>
<tr>
<td><input type="text" maxlength="7" size="7" name="coil_id" value="coil_22"></td>
<td><input type="text" size="7" name="width" value="220"></td>
<td><input type="text" size="5" name="gauge" value="330"></td>
</tr>
<tr>
<td><input type="text" maxlength="7" size="7" name="coil_id" value="coil_33"></td>
<td><input type="text" size="7" name="width" value="320"></td>
<td><input type="text" size="5" name="gauge" value="330"></td>
</tr>
</tbody>
</table>
</form>
</body>
</html>我有一个动态的行添加表,可以向表中添加行。我将它显示在3行中,它可以是1行,也可以是4行,如果用户想要添加数据,则可以是n行。
我的问题是:没有单击submit按钮,我如何发送数据(coil_id)?
在本例中,我希望在不使用submit按钮的情况下向服务器发送一个coil_id (本例中为coil_id = ”coil_22”)。
这也类似于:检测如果用户输入一个长度为7个字符或数字的线圈(例如:coil_22或2C12345),那么它将向服务器提交“coil_22” (而不是coilL_11或coil_33),而不需要单击任何提交/发送按钮。
我用了一段时间的JavaScript / jQuery,有时还是觉得迷茫。
非常感谢您的帮助。
发布于 2013-04-27 01:52:59
您可以使用.keyup()来实现,只需为每个输入添加一个类即可,例如
<input type="text" class="coil_input" maxlength="7" size="7" name="coil_id" value="coil_11">
<input type="text" class="coil_input" maxlength="7" size="7" name="coil_id" value="coil_22">
<script>
$('.coil_input').keydown(function() {
if($.trim($(this).val()).length == 7){
var now_input_value = $(this).val();
$.post('file.php', {now_input: now_input_value}, function(){ alert('Data sent'); }).fail(function(){ alert('An error has ocurred'); });
}
});
</script>所有输入都必须具有相同的类
记住要将jQuery添加到文档中。
发布于 2013-04-27 01:54:09
下面是jquery ajax示例。序列化将创建生成的查询字符串,该字符串将发送到您的url。
var datastring = $("#yourForm").serialize();
$.ajax({
type: "POST",
url: "your url",
data: datastring,
dataType: "json",
success: function(data) { alert('all ok');}
});https://stackoverflow.com/questions/16242234
复制相似问题