当提交数据被ajax复制时显示alert()

时间:2016-10-07 04:20:57

标签: jquery html ajax forms

我有表格和jquery。我将通过ajax提交表格。



 
                
            $("form#form").submit(function (event) { //submit nama pihak vendor
                event.preventDefault();
                var formData = new FormData($(this)[0]);

              //validation in here if duplicate
              
                $.ajax({
                    url: 'vendor_inout/vendor_inout_crud.php', //case:"insert_barang_in"
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        console.log(data);
                        if(data == 0){
                            alert('failed');
                        }else{
                            alert("success");
                        }
                    }
                });
                return false;
            });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <form id='form'>
    <select name='model[]'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
    <br>
    <select name='model[]'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
<br>
<input type='submit' value='submit'>
    </form
  
  
  </body>
&#13;
&#13;
&#13;

如果name='model[]'的值相同,如何在将数据提交到file.php之前显示警报?警报必须在标记//validation in here if duplicate中。

其传递问题的虚拟文本,请勿阅读

3 个答案:

答案 0 :(得分:1)

试试这个:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <form id='form'>
    <select name='model[]' id='model1'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
    <br>
    <select name='model[]' id="model2">
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
<br>
<input type='submit' value='submit'>
    </form>


  </body>


$("form#form").submit(function (event) { //submit nama pihak vendor
       var formData = new FormData($(this)[0]);
        event.preventDefault();
        var model1 = $("#model1").val();
        var model2 = $("#model2").val();

      //validation in here if duplicate
       var flag = 0;
       if (model1 == model2) { flag++; }
      if (flag > 0) { 
        alert("Duplicate"); 
       } else {
        $.ajax({
            url: 'vendor_inout/vendor_inout_crud.php', //case:"insert_barang_in"
            type: 'POST',
            data: formData,
            async: false,
            cache: false,
            contentType: false,
            processData: false,
            success: function (data) {
                console.log(data);
                if(data == 0){
                    alert('failed');
                }else{
                    alert("success");
                }
            }
        });
      }
        return false;
    });

答案 1 :(得分:0)

尝试一次

$("form#form").submit(function (event) { //submit nama pihak vendor
                event.preventDefault();
                var formData = new FormData($(this)[0]);

              //validation in here if duplicate
              
                $.ajax({
                    
                    url: 'vendor_inout/vendor_inout_crud.php', //case:"insert_barang_in"
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (data) {
                        console.log(data);
                        if(data == 0){
                            alert('failed');
                        }else{
                            alert("success");
                        }
                    }
                });
                alert('Before');
                return false;
                ;
            });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<body>
  <form id='form'>
    <select name='model[]'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
    <br>
    <select name='model[]'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
<br>
<input type='submit' value='submit'>
    </form
  
  
  </body>

答案 2 :(得分:0)

根据您对@Samudrala Ramu的评论回答:请使用以下代码。

如果所有组合框中的值相同isAllSame函数将返回true,否则它将返回false。

function isAllSame()
{

   var model=document.getElementsByName("model[]");
   for(var i = 1; i < model.length; i++)
   {
      if(model[i].value !== model[0].value)
        return false;
   }
   return true;
}

$("form#form").submit(function (event) { //submit nama pihak vendor
            event.preventDefault();
            var formData = new FormData($(this)[0]);

           if(isAllSame())
           {
              alert(" All Selected value are same");
              return false;
           }
          //validation in here if duplicate
          
            $.ajax({
                url: 'vendor_inout/vendor_inout_crud.php', //case:"insert_barang_in"
                type: 'POST',
                data: formData,
                async: false,
                cache: false,
                contentType: false,
                processData: false,
                success: function (data) {
                    console.log(data);
                    if(data == 0){
                        alert('failed');
                    }else{
                        alert("success");
                    }
                }
            });
            return false;
        });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id='form'>
    <select name='model[]'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
    <br>
    <select name='model[]'>
      <option value=''></option>
      <option value='model-a'>model-a</option>
      <option value='model-b'>model-b</option>
      <option value='model-c'>model-c</option>
      </select>
<br>
<input type='submit' value='submit'>
</form>