4

我对javascript非常陌生,因此对于我提出问题的方式有任何问题,我提前道歉。如果未填写所有字段,我正在尝试发布数据并弹出警告。其中一个字段是无线电类型。这是一个带有我的脚本http://jsfiddle.net/J2yWQ/64/的 jsfiddle 的链接

这是我目前拥有的

function emailWarning() {
    var check = document.getElementById("check");
    check.className = 'show';
}

function validateEmail(xem) {
    var re = /\S+@\S+\.\S+/;
    return re.test(xem);
}

function postData() {
        email = 'email'+document.getElementById('email').value;
    var tt = validateEmail(email);
    if (tt == true) {
        xmlhttp.open('POST', 'payment.php', true);
        xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
        xmlhttp.send(myProps.join("&"));
    } else {
        emailWarning();
    }
}

function insert() {
    try {
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        }


    var myProps = [];

    function addProp(id) {
            var value = encodeURIComponent(document.getElementById(id).value);
            myProps.push(id + "=" + value);
    }

    addProp('child_name');
    addProp('age');
    addProp('hometown');
    addProp('boy_girl');
        addProp('first_name');
    addProp('last_name');
    addProp('email');
    addProp('address1');
    addProp('address2');
    addProp('city');
    addProp('state');
    addProp('zip');
    addProp('country');

    var flagInvalid = false;
    var tempArray = document.getElementsByClassName("required");
    for (var i = 0; i < tempArray.length; i++) {
        if (tempArray[i].value == "") {
            flagInvalid = true;
            break;
        }
    }

    if (flagInvalid == false) {
        postData();

    } else {

        var log = document.getElementById("log");
        log.className = 'show';
        var log1 = document.getElementById("log1");
        log1.className = 'show';
        var log2 = document.getElementById("log2");
        log2.className = 'show';
        var log3 = document.getElementById("log3");
        log3.className = 'show';
        var log4 = document.getElementById("log4");
        log4.className = 'show';
        var log5 = document.getElementById("log5");
        log5.className = 'show';
        var log6 = document.getElementById("log6");
        log6.className = 'show';
        var log7 = document.getElementById("log7");
        log7.className = 'show';
        var log8 = document.getElementById("log8");
        log8.className = 'show';
        var log9 = document.getElementById("log9");
        log9.className = 'show';
        var log0 = document.getElementById("log0");
        log0.className = 'show';
        var logA = document.getElementById("logA");
        logA.className = 'show';
            }   

    } catch (e) {
        alert('An error occured in inert: ' + e);
    }

}
4

4 回答 4

2

addPropbody 更改为此问题时,很容易发现问题:

function addProp(id) {
  var el = document.getElementById(id);
  if (el) {
    myProps.push(id + "=" + encodeURIComponent(el.value));
  }
  else {
    alert('Not found: ' + id);
  }
}

此HTML中不存在boy_girl和ID:email

Boy: <input type="radio" name="boy_girl" id="boy_girl_b" value="boy"/>
Girl:<input type="radio" name="boy_girl" id="boy_girl_g" value="girl"/></li>
...
<input type="text" name="email" id="check" maxlength="64" class="required" />

您可以使用以下方法修复它:

function addProp(name) {
    var els = document.getElementsByName(name);
    if (els.length) {
       myProps.push(name + "=" + encodeURIComponent(els[0].value));
    }
    else {
       alert('Not found: ' + name);
    }
}

但事实上,这只是故事的开始。myProps对函数来说是本地的,insert但在函数中被引用postData;无论实际填写了哪些字段,您都会显示所有字段的验证错误标志......此外,您的代码有点湿 - 例如,所有这些

            var log = document.getElementById("log");
            log.className = 'show';
            var log1 = document.getElementById("log1");
            log.className = 'show';
            ...

...可以很容易地转化为:

var showValidationError = function(id) {
    var el = document.getElementById(id);
    if (el) { 
        el.className = 'show'; 
    } 
    else { 
        alert('Missing element #' + id);
    }
}
...
showValidationError('log');
for (var i = 1; i < 10; i++) {
  showValidationError('log' + i);
}

我明白你对 JS 很陌生;但这不是关于新鲜度,而是关于组织你的代码。

于 2012-10-24T12:56:12.257 回答
1

我打赌其中一条 addProp 行不正确。

调试这个,看看抛出错误之前的最后一个 id 是什么。

function addProp(id) {
        console.log(id);
        var value = encodeURIComponent(document.getElementById(id).value);
        myProps.push(id + "=" + value);
}

当你这样做时,你会发现这条线

addProp('boy_girl');

将失败,因为没有 id

        <span id="log4" class="hidden" style="color:red"><b>*</b></span>
        Boy: <input type="radio" name="boy_girl" id="boy_girl_b" value="boy"/>
        Girl:<input type="radio" name="boy_girl" id="boy_girl_g" value="girl"/></li>

因此,让我们更改函数以检查 id,如果 id 不存在则检查名称。

function addProp(id) {
    var input = document.getElementById(id);
    if (!input) {
        input = document.forms[0][id];
    }
    var value = encodeURIComponent(input.value);
    myProps.push(id + "=" + value);
}

更改它,使 myProps 在函数之外。

var myProps = [];  //<--- Added this line
function insert() {
        try {
            if (window.XMLHttpRequest) {
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
            }


            myProps = [];  //<---- Changed this line

            function addProp(id) {
                var input = document.getElementById(id);
                if(!input) {
                   input = document.forms[0][id];   
                }
                console.log(input);
                var value = encodeURIComponent(input.value);
                myProps.push(id + "=" + value);
            }

            addProp('child_name');
于 2012-10-24T12:54:25.707 回答
1

如果您收到此类错误:

如果它们不返回 null,请始终检查您的所有 document.getElementById

var node = document.getElementById('someid');

if(node){
    do something;
}

但请注意

var node = document.getElementById('someid').value;

或者

var node = document.getElementById('someid');

if(node.value){ 
    do something;
}

仍然可以抛出错误“无法读取 null 的属性”,因为您不检查节点是否真的存在

于 2012-10-24T13:27:25.700 回答
0

确保您设计的 ID 存在于页面上!比如boy_girl不存在!只有boy_girl_bboy_girl_g 这同样适用于邮件。这个有 idcheck而不是 mail。

使用 Opera Dragonfly 或类似工具进行基本故障排除

于 2012-10-24T13:01:30.673 回答