如何在多个输入字段上键入时更改值id?

时间:2012-01-12 04:27:53

标签: jquery input

这是代码:

<input type="text" id="participant1"  name="name[1][]" value="Andi"/> </p>
<input type="text" id="idparticipant1" name="idparticipant[1][]" value="1001"/>

<input type="text" id="participant2"  name="name[2][]" value="Smith"/> </p>
<input type="text" id="idparticipant2" name="idparticipant[2][]" value="1005" />

这是jquery脚本:

<script src="jquery-1.4.js"></script>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="jquery.autocomplete.css" />
<script>
$(document).ready(function()
{

    $('[id^="participant"]').keyup(function()
{
      var txt = $(this).val();
      $.ajax({
  type: "POST",
  url: "blue.php",
  data: "nameparticipant=" + txt,
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  success: function(data)
  {
     $(this).next().val(data.d);
     //alert(data.d); 
  }
  //failure: failerEvent
});
});//keyup

 });
 </script>

blue.php的脚本

<?php
$nameparticipant=$_POST["nameparticipant"]);
$res = mysql_query("select * from table where upper(name) like '$nameparticipant%'");
$t=mysql_fetch_array($res);

echo"$t[nik]";
?>

当我输入名称“Andy”以外的参与者的输入名称时,我想将值id改为0.当我再次将其翻过来时,其值id改为开头。

我该怎么办?

1 个答案:

答案 0 :(得分:1)

使用

var txt = $(this).val();

而不是

var txt=$( '[id^="participant"]' ).val();

从代码

中删除不必要的<p>标记
<input type="text" id="participant1"  name="name[1][]" value="Andi"/>
<input type="text" id="idparticipant1" name="idparticipant[1][]" value="1001"/>

<input type="text" id="participant2"  name="name[2][]" value="Smith"/> 
<input type="text" id="idparticipant2" name="idparticipant[2][]" value="1005" />

的JavaScript,

$.ajax({
    type: "POST",
    url: "blue.php",
    data: "nameparticipant=" + txt,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data)
         {
            $(this).next().val(data.d); 
         }
});

working sample