从使用JQuery和AIR隐藏的输入中获取值String

时间:2010-11-11 10:38:15

标签: jquery string air

我的代码有问题..当我得到问题[i]的价值时.. 例如:如果我有一个从数据库中获取并在表格行中显示的问题,比如'你的名字是什么',那么我就把它放入一个隐藏的类型......

当我发布该表格时...我回复问题..它必须'你叫什么名字'但我只得到第一个字符串'什么'..任何人都可以修复我的代码..或纠正错误

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Untitled Document</title>
 </head>
 <body>
  <form name="savequestion" action="#" method="post">
   <div id="readDatabase">
    <table id="questionlist"></table>
   </div>
  </form>


  <script type="text/javascript" src="js/jquery.js"></script>
   <script src="js/AIRAliases.js" />
   <script src="js/database.js" />
  <script type="text/javascript">

   $(document).ready(function(){
    db = SetupDB();
    read_questions();
    $('#save').click(function(){
     var count_data=$("#countof_record").val();
     for (i = 0; i < count_data; i++) {
      //get value of question+i
      **alert($("#question"+i).val());**
//------------------------------i will get question here, but i only get : first string of question----------------------------
     }

    });
   });

   function read_questions(){
    dbQuery = new air.SQLStatement(); 
    dbQuery.sqlConnection = db;  
    dbQuery.text = "SELECT * FROM t_Questions";  
    try {
     dbQuery.execute();
    } 
    catch (e) {
     alert(e.message); 
    }
    results = dbQuery.getResult();
    //$("#questionlist tbody").empty();
    for (i = 0; i < results.data.length; i++) {  
      row = results.data[i];  
      var questions=row.ContentQuestion;
      alert(questions);
     $("#questionlist").append("<tr id=rowQuestion><td>"+(i+1)+"</td><td><input type=hidden name=question"+i+" id=question"+i+" value="+questions+" size=255>"+row.ContentQuestion+"</td><td><select name=answer"+i+" id=answer"+i+"><option name=answerA value=A selected>"+row.OptionAQuestion+"</option><option name=answerB value=B>"+row.OptionBQuestion+"</option><option name=answerC value=C>"+row.OptionCQuestion+"</option><option name=answerD value=D>"+row.OptionDQuestion+"</option></select></td></tr>");  
    }  
    $("#questionlist > tbody:last").append("<tr><td><input type=hidden name=countof_record id=countof_record value="+results.data.length+"><input type=submit name=submit value=Save your answer id=save></td></tr>");
   }  
  </script>
 </body>
</html>

1 个答案:

答案 0 :(得分:0)

您在所有属性上都缺少引号,并且您正在重新使用rowQuestion ID来纠正此问题。 始终将您的属性包装在引号中。


作为当前方法的替代方法check out jQuery templates,您的代码模板将如下所示(我希望您同意,也更容易维护):

<script id="rowTmpl" type="text/x-jquery-tmpl">
  {{each items}}
  <tr class="rowQuestion">
    <td>${$index+1}</td>
    <td><input type="hidden" name="question${$index}" id="question${$index}" value="${$value.ContentQuestion}">${$value.ContentQuestion}</td>
    <td>
      <select name="answer${$index}" id="answer${$index}">
        <option name="answerA" value="A">${$value.OptionAQuestion}</option>
        <option name="answerB" value="B">${$value.OptionBQuestion}</option>
        <option name="answerC" value="C">${$value.OptionCQuestion}</option>
        <option name="answerD" value="D">${$value.OptionDQuestion}</option>
      </select>
    </td>
  </tr
  {{/each}}>
  <tr>
    <td>
      <input type="hidden" name="countof_record" id="countof_record" value="${items.length}">
      <input type="submit" name="submit" value="Save your answer" id="save">
    </td>
  </tr>
</script>

要使用它,你可以替换它:

for (i = 0; i < results.data.length; i++) {  
  row = results.data[i];  
  var questions=row.ContentQuestion;
  alert(questions);
  $("#questionlist").append("<tr id=rowQuestion><td>"+(i+1)+"</td><td><input type=hidden name=question"+i+" id=question"+i+" value="+questions+" size=255>"+row.ContentQuestion+"</td><td><select name=answer"+i+" id=answer"+i+"><option name=answerA value=A selected>"+row.OptionAQuestion+"</option><option name=answerB value=B>"+row.OptionBQuestion+"</option><option name=answerC value=C>"+row.OptionCQuestion+"</option><option name=answerD value=D>"+row.OptionDQuestion+"</option></select></td></tr>");  
}  
$("#questionlist > tbody:last").append("<tr><td><input type=hidden name=countof_record id=countof_record value="+results.data.length+"><input type=submit name=submit value=Save your answer id=save></td></tr>");

有了这个:

$("#rowTmpl").tmpl({items: results.data}).appendTo("#questionlist");