0

I'm trying use this function to prevent the duplicate but only work for prevent not to break action. How to make this function work not only for prevent but for break action too ?

            function AddTipeTruk() {
        var form = $("#formtruk");            
        var contents = {},
            duplicates = false;
        $("#table-custtiptruk td").each(function () {
            var tdContent = $(this).text();
            if (contents[tdContent]) {
                duplicates = true;
                return false;
            }
            contents[tdContent] = true;
        });
        if (duplicates) {
            alert("There were duplicates.");                
        }
        if (form.valid()) {
            var markup = "";
            markup = "<tr><input id='TipTrukId' name='TipTrukId' type='hidden' value='1'><td style='display:none;'><input value='" + 1 + ";" +
                 +$('#JenisTruck option:selected').val() + ";" + $('#Alias').val() + ";" +
                "' name='TipTrukData'/> </td><td class='no'></td><td>" + $('#JenisTruck option:selected').text() + "</td><td>" + $('#Alias').val() + "</td>" +
                   "<td><a href='#' data-toggle='modal' onclick='EditTipTrukRow($(this))'>Edit</a> | <a href='#' onclick='RemoveTipTrukRow($(this));'>Delete</a></td></tr>";
            $("#table-custtiptruk tbody").append(markup);
            updateRowNumberTipTruk();
            $('#modal').modal('hide');
        }                             
    }
1
  • Add onsubmit="javascript:AddTipeTruck()" to your form and return false in this function to stop submitting Commented Jul 21, 2016 at 9:12

1 Answer 1

1

You are jumping from inner loop. You need to jump from function too. See below.

$("#table-custtiptruk td").each(function () {
            var tdContent = $(this).text();
            if (contents[tdContent]) {
                duplicates = true;
                return false;
            }
          if(duplicates ==true){
              return !duplicates;
          }
            contents[tdContent] = true;
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.