如何使用jquery从数据表中设置选项和单选按钮的值

时间:2016-04-09 15:18:55

标签: jquery datatables

请帮忙,我正在使用jQuery Datatables插件,当我想编辑它并发送到模态对话框时,我需要设置一个选择选项值“selected”的值和单选按钮“checked”ass来自数据库/数据表。这是我的示例代码

<?php
    //Connection Database
    $con = mysqli_connect("localhost","root","","diliport");
    if (mysqli_connect_errno()) {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    switch ($_POST['type']) {
        //Show Data
        case "get":
            $SQL = mysqli_query($con, "SELECT * FROM passageiro WHERE id='".$_POST['id']."'");
            $return = mysqli_fetch_array($SQL,MYSQLI_ASSOC);
            echo json_encode($return);
            break;
    } 
?>

function showModals(id) {
  waitingDialog.show();
  clearModals();
  if (id) {
    $.ajax({
      type: "POST",
      url: "crud.php",
      dataType: 'json',
      data: {
        id: id,
        type: "get"
      },
      success: function(res) {
        waitingDialog.hide();
        setModalData(res);
      }
    });
  }
  // for new add new data
  else {
    $("#myModals").modal("show");
    $("#myModalLabel").html("New User");
    $("#type").val("new");
    waitingDialog.hide();
  }
}

//To show data in modal for edit 
function setModalData(data) {
  $("#myModalLabel").html(data.Nome_passageiros);
  $("#id").val(data.id);
  $("#type").val("edit");
  $("#nome").val(data.Nome_passageiros);
  $('input:radio[name=sex]').val(data.Sexo).checked = true;
  $("#kelas option:selected").val(data.Destinasaun); //don't want to show data to option value selected                                             
  $("#myModals").modal("show");
}
<form action="" role="form" data-toggle="validator" id="formPassageiro" method="post" class="form-horizontal">
  <input type="hidden" class="form-control" id="id" name="id">
  <input type="hidden" class="form-control" id="userid" name="userid">
  <input type="hidden" class="form-control" id="type" name="type">
  <thead>
    <div class="row">

      <div class="form-group">
        <label for="naran" class="col-md-2 col">Naran Passageiro</label>
        <input type="text" name="nome" id="nome" placeholder="naran" class="col-md-5 col, highlight" required="" size="50" />
        <label for="sexo" class="col-md-1 col">Sexo :</label>
        <input type="radio" name="sexo" id="sexo" value="Mane" />
        <label for="mane">&nbsp;Mane &nbsp;&nbsp;</label>
        <input type="radio" name="sexo" id="sexo" value="Feto" />
        <label for="feto">&nbsp; Feto</label>
      </div>
    </div>

    <div class="row">
      <div class="form-group">
        <label for="Destinado" class="col-md-2 col">Destinasaun</label>
        <label class="style-label">
          <select class="form-group,col" name="kelas" id="kelas" onchange="ambildata(this.value)+ambilharga(this.value)" size="">
            <option value=""></option>
          </select>
        </label>


  </thead>

</form>

1 个答案:

答案 0 :(得分:0)

最后解决

doInBackground()