如何根据值验证表单并相应地将标注提交到另一个页面?

时间:2019-01-05 02:48:34

标签: javascript

我如何暂停提交,以便可以在最终提交表单之前基于所有选择进行验证,检查值并可能提交xhtml标注?

尝试使用这里找到的各种jquery方法,使用回调,设置超时并保持while循环直到一切完成。在代码中找到的许多警报仅用于故障排除/跟踪。

function completeTicket() {
    alert("fnOpenDiag Called");
    $("#dialog-confirm").html("Auto create return eTicket?");

    // Define the Dialog and its properties.
    $("#dialog-confirm").dialog({
        resizable: false,
        modal: true,
        title: "Auto create return eTicket?",
        height: 250,
        width: 400,
        buttons: {
            "Yes": function () {
                var quickissue = "Return <?php echo ($ticketRow['items_count'] >= 1 ? $ticketRow['items'] : 'Computer'); ?>";
                var selectedLocation2 = <?php echo ($ticketRow['Location_ID'] == '' ? 0 : $ticketRow['Location_ID']); ?>;
                xmlhttp=GetXmlHttpObject();
                if (xmlhttp==null) {
                  alert ("This browser does not support XMLHTTP!");
                }

                var url="xhtmlCallOut_QuickEticket.php?callerID=pc_ticket_detail&selectedLocation_ID=" + selectedLocation2 + "&tboxIssue=" + quickissue;

                xmlhttp.open("GET",url,false);
                xmlhttp.send(null);
                if (xmlhttp.readyState==4){
                    if (xmlhttp.responseText != 0){
                        alert(xmlhttp.responseText);
                    }
                }
                alert("ticket success");
                return true;
                $(this).dialog('close');
                //callback(true);
                //callback();
            },
                "No": function () {
                return true;
                $(this).dialog('close');
                //callback(false);
                //callback();
            }
        }
    }); 
}

function checkForm() {
      alert("checkform called");
      if(document.getElementById('assocCompany').selectedIndex == 0) {
          alert('Please associte this company with a known company name\nfrom the drop list.');
          document.getElementById('assocCompany').focus();
          e.preventDefault();
          return false;
      }
      else if(document.getElementById('assignTech').selectedIndex == 0 && document.getElementById('status').selectedIndex >= 2){
          alert('You must define a technician first!');
          document.getElementById('assignTech').focus();
          e.preventDefault();
          return false;
      }
      else if(RegisterForm.elements['status'].value == 3 && RegisterForm.elements['tboxreaspend'].value.length < 3){
          alert('You must give a reason for this ticket being changed to pending');
          document.getElementById('tboxreaspend').focus();
          e.preventDefault();
          return false;
      }
      else if(RegisterForm.elements['tboxissue'].value.length < 3){
          alert('You must give a description of the issue');
          document.getElementById('tboxissue').focus();
          e.preventDefault();
          return false;
      }
      else {
          pc_ticketdetail.actionbutton.disabled=true;
          return false;
      }
}

function showPendingReason() {
    var y = document.getElementById("status");
    var val = y.options[y.selectedIndex].value;
    if (val == 3) {
        $('#trReasPend').show();
    } else {
        $('#trReasPend').hide();
    }
}

function submitForm() {
    alert("submitform called");
    var x = document.getElementById("status");
    var valx = x.options[x.selectedIndex].value;
    alert("statval is " + valx);
    if (valx == 4) {
        if (completeTicket()) {
            e.preventDefault();
            return false;
            alert("complete ticket done");
        } else {
            e.preventDefault();
            return false;
        }
    } else {
        if (checkForm()) {
            e.preventDefault();
            return false;
            alert("checkform done");
        } else {
            alert("checkform FALSE return");
            e.preventDefault();
            return false;
        }
    }
}

<div id="dialog-confirm"></div>

<form action="<?php $_SERVER['PHP_SELF']; ?>" name="pc_ticketdetail" id="pc_ticketdetail" method="post" onSubmit="return submitForm();">

<select name="status" id="status" onChange="javascript:showPendingReason();">
          <option<?php if($ticketRow['status'] == 1){ echo ' selected'; } ?> value="1">New</option>
          <option<?php if($ticketRow['status'] == 2){ echo ' selected'; } ?> value="2">Bench</option>
          <option<?php if($ticketRow['status'] == 3){ echo ' selected'; } ?> value="3">Pending</option>
          <option<?php if($ticketRow['status'] == 4){ echo ' selected'; } ?> value="4">Finished</option>      
          <?php if($ticketRow['status'] == 5){ 
              echo '<option selected value="5">Closed/Deleted</option>';
           } ?> 
        </select>

当前似乎可以按预期完成任务,只是“ completeTicket”和“ checkForm”函数未调用或未正确返回。表单只是在他们未通过验证或打开模式对话框并要求创建回程票时提交并关闭。

3 个答案:

答案 0 :(得分:0)

看看javaScript中的(async-await)函数。 async function

答案 1 :(得分:0)

您的checkForm()永远不会返回false,因此completeTicket()也永远不会返回true。您可以首先将变量定义为false,如果满足特定条件,则将其设置为true,然后将该变量返回给主函数。

categoryAxis.tooltip.label.adapter.add("textOutput", function(text) {
  if (text && text.length <= categoryCharacterLimit) {
    return "";
  }
  return text;
});

答案 2 :(得分:0)

如果从return语句下删除警报,这将有助于您查找问题,因为从函数返回后将不会运行任何代码。 $(this).dialog('close')也是如此。

我认为您应该在e.preventDefault()中使用事件对象之前捕获该事件对象,因此请尝试从表单中删除onSubmit属性,并通过jQuery这样添加侦听器:

$(YOUR_FORM_SELECTOR).submit(function(e) {
  // here you access to the e Object
})

我不知道这是否与您遇到的问题有关,请尝试在运行代码时准确描述正在发生的事情。