【问题标题】:unselected options in select box does not append into another select box选择框中未选择的选项不会附加到另一个选择框中
【发布时间】:2012-12-26 19:10:43
【问题描述】:

我的应用程序中有 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();
        }
    }
  });        
}

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

$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"));
}

【问题讨论】:

  • 这不是预期的行为吗?为什么不在服务器上解决这个问题?
  • @mplungjan php 插入看起来很好。但我不确定,但在发布用于插入的数据时,它是否只发布选择框中的选择选项?我也想在选择框中发布未选择的选项。实际上我想要的是#studentadd选择框中的任何选项都应该附加到第二个选择框中并插入到数据库中,无论是否在选择框中选择(突出显示)

标签: php jquery html ajax mysqli


【解决方案1】:

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

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

或者你的意思是

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

??

【讨论】:

  • #studentadd,这是非常聪明的想法。谢谢你的回答:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-17
  • 2017-04-20
  • 1970-01-01
相关资源
最近更新 更多