如何防止焦点输出在文本输入字段

时间:2017-03-11 21:16:59

标签: javascript jquery

我想防止专注于在文本输入字段上创建额外的更改事件。

我在使用keyup()键入文本输入字段时验证表单,并在使用ajax输入时更改()事件。

当我点击“保存”按钮时,它会再次验证表单,如果确定将表单保存到数据库。

当我在文本字段上键入时,问题发生,将光标留在那里并单击保存按钮。这首先生成更改事件(因为聚焦)并验证表单,因为单击了保存,它保存一次,然后执行保存按钮单击事件,这将再次保存表单。

HTML

<form id="scheduling" name="scheduling" method="post">
    <div id="prodSchedContainer">
        <div>
            <input type="text" name="inputText" id="tInput-1" class="testInputField">
        </div>
    </div>
    <button type="button" id="saveButton-scheduling" name="saveButton" class="sharedButton saveButton" title="Save">
</form>

JS:

$('#prodSchedContainer').on('change', '.testInputField', function () {
    infoLine = 'TEST INPUT CHANGED:    >>>>>     id: ' + $(this).attr('id') + '     >>>>>     ' + $(this).val();
    renderInfoBox(infoLine);
    performScheduleValidation($(this));
});

$('#prodSchedContainer').on('keyup', '.testInputField', function () {
    infoLine = 'TEST INPUT KEYUP:    >>>>>     id: ' + $(this).attr('id') + '     >>>>>     ' + $(this).val();
    renderInfoBox(infoLine);
    performScheduleValidation($(this));
});


$('#schedulingContainer').on('click', '.saveButton', function () {
    infoLine = 'SAVING SCHEDULE:    >>>>>     id:' + $(this).attr('id') + '     >>>>>     ' + 'WHAT:' +     $(this).attr('what') + '   / TYPE:' + $(this).attr('apptType') + ' / APPT:' + $(this).attr('appt') + '      >>>>>    ' + $(this).val();
    renderInfoBox(infoLine);
    performScheduleValidation($(this));
});

function performScheduleValidation(sObj) {
    objID = $(sObj).attr('id');
    if (objID == 'saveButton-scheduling') {
        action = 'save';
    } else {
        action = 'validate';
    }
    valData.push(['action', action]);
    valData.push(['formData', formData]);

    var ajaxData = JSON.stringify(valData);
    var URL = BASE_DIR + 'schedulingValidation.php';

    $.ajax({
        url: URL,
        type: 'POST',
        data: { data: ajaxData },
        success: function (theResponse) {
            var responseData = $.parseJSON(theResponse);
            isValid = responseData['valid'][0];
            if (!isSaved) { //---   If the Schedule is not Saved
                //  Do some stuff within the form
            } else {    //---   The Schedule Is saved   ----------  /
                $('#schedulingContainer').html('');         //----  Clear up the scheduling container       /
                $('#product_idx_selector').val(0);
                count_db_table('ota_scheduling');
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert('Error: ' + errorThrown);
        }
    });
}

PHP:

require_once("includes/connection.php");
require_once("/includes/functions.php");
$lang  = $_SESSION['Language'];

$data = json_decode($_POST['data']);

$action         = $data[0][1];
// ---- More Data
$weeklySchedule = $data[9][1];
$objID          = $data[10][1];

//---   Validation Part
$schVal = 1;    // This is 1 or 0 depending on the validation calculations

//---   SAVING Part
if ($action == 'save' && $objID=='saveButton-scheduling' && $schVal == 1) {
    $query  = "INSERT INTO ota_scheduling ( ";
    $query .= "product_idx, ";
    $query .= "trainers, ";
    $query .= "coupons ";
    $query .= ") ";

    $query .= "VALUES ( ";
    $query .= $productRow['id'] . ", ";
    $query .= "'".$multiAppointments[0]['trainers'] . "', ";
    $query .= "'".$multiAppointments[0]['coupons'] . "' ";
    $query .= " )";

    $result = mysql_query($query, $connection);
    if(!$result) { 
        die("Database query failed at 'schedulingValidation.php' while Adding a Parent Schedule @ ota_scheduling: " . mysql_error());
    } else { 
        $parentID = mysql_insert_id();
    }
}

0 个答案:

没有答案