0

我的应用程序中有 2 个选择框。现在,当用户提交页面时,所有在选择框中被选中(突出显示)的学生#studentadd都会被附加到#studentselect选择框。但未在第一个选择框中选择的选项不会附加到第二个选择框中。

我的问题是如何在“#studentadd”选择框中获得未选择的选项以附加到“#studentselect”选择框中?

是我的 php/ajax 的问题,因为我所做的是使用 ajax 导航到单独的 php 文件并将#studentadd选择框中的学生插入数据库。但是插入只发生在#studentdd选择框中的那些学生荧光笔上。它不会对那些在 `#studentadd' 中未选择的选项执行插入操作。

下面是选择框#studentadd

<select multiple="multiple" name="addtextarea" id="studentadd" size="10">
    <option value='1'>u08743 - Joe Cann</option>
    <option value='4'>u03043 - Jill Sanderson</option>
    <option value='7'>u08343 - Craig Moon</option>
</select>

以下是学生应附加到的选择框:

<select id="studentselect" name="studenttextarea"></select>

下面是jquery/ajax:

 function submitform() {    

    $.ajax({
        type: "POST",
        url: "updatestudentsession.php",
data: { 
    addtextarea:$('#studentadd').val()
        },
        dataType:'json',  //get response as json
        success: function(result){
                    if(result.errorflag){

       //do your stuff on getting error message
      var newHtml="<span style='color: red'>"+result.msg+"</span>"; 
      $("#targetdiv").html(newHtml);  //i am displaying the error msg here

      $('#targetdiv').show();

    }else{
       //you got success message

       var newHtml="<span style='color: green'>"+result.msg+"</span>"; 
            $("#targetdiv").html(newHtml);


           //append students you want to add to assessment into student exist select box 
            var selectedOption = $('select#studentadd');
            $('select#studentselect').append(selectedOption.html()); 

             //blank #studentadd select box
             $('#studentadd').empty(); 

                $('#targetdiv').show();
        }
    }
  });        
}

下面是updatestudentsession.php从 ajax 访问并将数据插入数据库的单独 php 文件:

$studentid = (isset($_POST['addtextarea'])) ? $_POST['addtextarea'] : array(); 
$sessionid = (isset($_POST['Idcurrent'])) ? $_POST['Idcurrent'] : array();   

$insertsql = "
INSERT INTO Student_Session
(SessionId, StudentId)
VALUES
(?, ?)
";

if (!$insert = $mysqli->prepare($insertsql))
{
    // Handle errors with prepare operation here
}      

$success = true;

foreach($studentid as $id)
{ 
    $insert->bind_param("ii", $sessionid, $id);

    if($insert->execute() === false)
    {
        $success = false;
    }
}

$insert->close();

if($success)
{
    echo json_encode(array('errorflag'=>false,'msg'=>"Students have been successfully added into the Assessment"));
}
else
{
    echo json_encode(array('errorflag'=>true,'msg'=>"An error has occured, Students have not been added into the Assessment"));
}
4

1 回答 1

1

啊,您想在提交时选择所有目标选项:

function submitform() {    
  $('#studentadd option').attr('selected', 'selected');

还是你的意思

  $('#studentselect option').attr('selected', 'selected');

??

于 2013-01-12T08:45:53.343 回答