duanma8207 2013-01-12 07:01
浏览 34
已采纳

选择框中未选中的选项不会附加到另一个选择框中

I have 2 select boxes in my application. Now what happens that when the user submits the page, all students that have been selected (highlighted) in select box #studentadd gets appended to #studentselect select box. But options which have not been selected in the first select box would not get appended to the second select box.

My question is how can I also get unselected options within '#studentadd' select box to be appended into '#studentselect' select box?

Is the issue with my php/ajax because what I do is use ajax to navigate to the seperate php file and insert the students in the #studentadd select box into the database. But the insert only occur for those students highlighter in the #studentdd select box. It does not perform inserts on those options where were not selected in `#studentadd'.

Below is the select box #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>

Below is the select box the students should be appended into:

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

Below is 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();
        }
    }
  });        
}

Below is the separate php file updatestudentsession.php which gets accessed from the ajax and inserts the data into the database:

$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"));
}
  • 写回答

1条回答 默认 最新

  • dongpo0409 2013-01-12 08:45
    关注

    Ah, you want to select all the target options on submit:

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

    or did you mean

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

    ??

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)