将输出JSON发送到MTurk提交

时间:2017-09-07 23:00:49

标签: javascript html mechanicalturk

我有一个HTML表格,可以由MTurk编辑以进行数据收集。我有一个函数构建来将表变成一个json,以便它出现在每一个HIT的一行,除了我不知道如何提交这个。我已成功将数据聚合到json,然后将其记录在控制台中。我是HTML和Javascript的新手。理想的答案将清楚地演示如何将数据提交回Sandbox中的亚马逊。

的Javascript

  <script language='Javascript'>
   function deleteRow(row)
{
    var i=row.parentNode.parentNode.rowIndex;
    document.getElementById('Table').deleteRow(i);
}


   function addRow(row)
{
    var i = row.parentNode.parentNode.rowIndex;
    var tr = document.getElementById('Table').insertRow(i+1);
    tr.innerHTML = row.parentNode.parentNode.innerHTML;
    tr.children[0].innerHTML = tr.parentNode.querySelectorALL("tr").length-1;
} 

 $('#convert-table').click(function()
 {
    var table = $('#Table').tableToJSON(
        {
            onlyColumns:[0,1,2],
            extractor:function(cellIndex, $cell) {
              return $cell.find('input').val();
            }
        });
    console.log(table);
    alert(JSON.stringify(table));
 });


 </script>

HTML

<HTMLQuestion xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2011-11-11/HTMLQuestion.xsd">
  <HTMLContent><![CDATA[
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
  <script type='text/javascript' src='https://s3.amazonaws.com/mturk-public/externalHIT_v1.js'></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://storage.googleapis.com/jquerytabletohtml/jquery.tabletojson.js"></script>
 </head>
 <body>
  <form name='mturk_form' method='post' id='mturk_form' action='https://www.mturk.com/mturk/externalSubmit'>
  <input type='hidden' value='' name='assignmentId' id='assignmentId'/>

  <h1 align="center">Insertion of Missing Data</h1>
  <h2>Instructions:</h2>
    <p>Open the document <a href="https://storage.googleapis.com/directionalsurvey/Amazon%20Turk%20Test%20files/PDFS/100122514.pdf" target='_blank'>HERE</a> and look for a missing value by scanning a column in the pdf until the data shown below does not line up. when you find one insert a row and enter the three variables Measured Depth, Inclination, and Azimuth.</p>
    <p> The variables could appear as: </p>
      <ul>
        <li><b>Measured Depth</b></li>
          <ul><li>MD, Depth, Measured Depth, M.D.</li></ul>
        <li><b>Inclination</b></li>
          <ul><li>Incl, IncAng, Inclination Angle, Inclination</li></ul>
        <li><b>Azimuth</b></li>
          <ul><li>Azi, Azimuth Angle, DirDeg, Directional Angle</li></ul>
      </ul>

    <p>I expect there to be <b>~ 5 </b>missing rows... when you are done hit submit.</p>

    <div id="tablediv">
        <table id="Table" border="1">
            <tr>
                <td><b>Measured Depth</b></td>
                <td><b>Inclination</b></td>
                <td><b>Azimuth</b></td>
                <td><b>Delete?</b></td>
                <td><b>Add Row?</b></td>
            </tr>
            <tr>
                <td><input size=25 type="text" id="Measured Depth0[]" contenteditable="true" value='339'></td>
                <td><input size=25 type="text" id="Inclination0[]" contenteditable='true' value='0.540000021'></td>
                <td><input size=25 type="text" id="Azimuth0[]" contenteditable='true' value='310.7200012'></td>
                <td><input type="button" id="delbutton0" value="Delete" onclick="deleteRow(this)"/></td>
                <td><input type="button" id="addmorebutton0" value="Add Row Below" onclick="addRow(this)"/></td>
            </tr>
            <tr>
                <td><input size=25 type="text" id="Measured Depth1[]" contenteditable="true" value='432'></td>
                <td><input size=25 type="text" id="Inclination1[]" contenteditable='true' value='0.930000007'></td>
                <td><input size=25 type="text" id="Azimuth1[]" contenteditable='true' value='326.3599854'></td>
                <td><input type="button" id="delbutton1" value="Delete" onclick="deleteRow(this)"/></td>
                <td><input type="button" id="addmorebutton1" value="Add Row Below" onclick="addRow(this)"/></td>
            </tr>
        </table>
    </div>
  <button id="convert-table">Convert!</button>
  <p><input type='submit' id='submitButton' value='Submit' /></p></form>    
 <script language='Javascript'>turkSetAssignmentID();</script>

 </body>
</html>
]]>
  </HTMLContent>
  <FrameHeight>900</FrameHeight>
</HTMLQuestion>

1 个答案:

答案 0 :(得分:1)

你真的很亲近。

正下方
<input type='hidden' value='' name='assignmentId' id='assignmentId'/>

元素,添加另一个:

<input type='hidden' value='' name='data' id='data'/>

然后,而不是做

alert(JSON.stringify(table));

这样做:

$('#data').val(JSON.stringify(table));

这将导致隐藏data输入元素的值保存table的值。当工作人员按下提交时,此隐藏字段将被传输到MTurk,并在您获得HIT时可用。