当数据通过ajax

时间:2016-07-28 08:01:31

标签: php jquery html ajax

我想通过使用if else条件在一个变量(check)中添加两个变量(类textbox和hiddentxtbox)值,并且我想在ajax中传递那个变量(check)。我想如果有人选择数据库头选项之类的话firstname,lastname然后下拉值通过复选框,否则textbox值通过复选框。在我的脚本中,我已经传递了检查变量,如下所示: -  data:{check:textbox,oldval:textbox1,hiddentxtbox:hiddentxtbox,olddropval:textbox2} 如上面的代码,只有check变量有一个值textbox。我想如果有人选择数据库标题选项,如firstname,lastname,则下拉值通过复选框,如check:hiddentxtbox else check:textbox

<script>
  $(document).ready(function(){
    $('#submit').click(function(){

    var  hiddentxtbox=[];
    var textbox = [];
    var textbox1 = [];
    var textbox2= [];

    var check = [];
    $('.check').each(function() {
      if ($(this).is(':checked')) {
        var current = $(this).val();

        //alert(current);
        textbox.push($(this).parents("tr").find(".textbox").val())
        textbox1.push($(this).parents("tr").find(".textbox1").val())
        textbox2.push($(this).parents("tr").find(".textbox2").val())
        hiddentxtbox.push($(this).parents("tr").find(".newhiddenBox").val())
      }
      check.push($(this).val());
    });
    $.ajax({
      url: 'practice.php',
      type: 'post',
      data: {check: textbox, oldval: textbox1, hiddentxtbox: hiddentxtbox, olddropval: textbox2},
      success: function (data) {
          alert(data);
      }
    });
    return false;
  });
});
</script>

      <table>

        <tr>
          <td>
            <select name='drop[]' class='select_drop textbox textbox1 form-control'>
              <option  value="firstname">firstname</option>
              <optgroup label='newtextfields' class='txtfields' >
                <option value='text' >text</option>
                <option value='number'>number</option>
              </optgroup>
              <optgroup label='database headers'> 
                <option value="firstname">firstname</option>
                <option value="lastname">lastname</option>
                <option value="email">email</option>
                }
              </optgroup>
          </td>

          <td><input  type='hidden' value='firstname'  class='textbox2  form-control' id='textbox2'></td>
          <td><input type='hidden' id='newhiddenBox' class='newhiddenBox' value='firstname'></td>
          <td><input type='checkbox'  name='check' value='firstname' class='check checkbox-primary' >Add Column</td> 
          <br>
          </select>
        </tr>
      </table>
    </div>


    <div class='form-group'>
      <table>
        <tr>
          <td>
            <select name='drop[]' class='drop  form-control' >
              <optgroup selected label='newtextfields'>
                <option value='text'>text</option>
                <option value='number'>number</option>
              </optgroup>
              <optgroup label='database headers' class='hd'>
                <option value="firstname">firstname</option>
                <option value="lastname">lastname</option>
                <option value="email">email</option>
              </optgroup>
            </select>
          </td>
          <td><input  type='text' value='lastname'  class='textbox tt form-control' id='textbox'></td>
          <td><input  type='hidden' value='lastname'  class='textbox1 form-control' id='textbox1'></td>
          <td><input type='hidden' id='newhiddenBox'class='newhiddenBox' value='lastname'></td>  
          <td><input type='checkbox' name='check'  value='lastname' class='check checkbox-primary' style='margin-left:5px;'>Add Column</td>
        </tr>
      </table>
    </div>
  </div>

</div>
<input type='submit' name='submit' id='submit' value='submit' >

</div>

0 个答案:

没有答案