使用jQuery提交按钮不能在IE9和FF9中工作

时间:2012-01-27 18:21:06

标签: javascript jquery html css cross-browser

此提交按钮适用于Safari和Chrome,但不适用于IE9或FF9。

提交按钮 -

 <img type="submit" src="lib/send_feedback.jpg" border="0" class="feedback-submit-img" onClick="javascript: validate(); return false;"/>

相关的jQuery -

// Submit form to next page
function submitForm() {
//   document.forms["feedbackform"].submit();
    document.feedbackform.submit();
}
// Submit form and validate email using RFC 2822 standard
function validateEmail(email) { 
    // Modified version original from: http://stackoverflow.com/a/46181/11236
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}
// Return true if email field is left unchanged
function originalText(email){
    var defaultMsg;
    defaultMsg = "Enter your email address (optional)";
    if(defaultMsg == email){
        return true;    
    }
    return false;
}
// Verify or decline with error message
function validate(){
    $("#result").text("");
    var email = $("#email").val();
    if ((validateEmail(email)) || originalText(email)) {
        w.value = screen.width;
        h.value = screen.height;
        submitForm();
    } else {
        $("#result").text(email + " is not a valid email.");
        $("#result").css("color", "red");
    }
    return false;
}
$("form").bind("submit", validate);

当我将提交按钮更改为更基本以确保提交w和h假设要更新的隐藏字段时,它的价值也是值得的。

Here is the entire codethe CSS

1 个答案:

答案 0 :(得分:3)

type不是img代码的有效属性:https://developer.mozilla.org/En/HTML/Element/Img

相反,我认为您需要<input type="image" />代码:https://developer.mozilla.org/en/HTML/Element/Input

<img type="submit" />对我来说不起作用,但使用了<input type="image" />http://jsfiddle.net/HXrNb/1/

然后,您可以将validate()函数绑定到表单的submit事件:

$('form').on('submit', function () {
    ...
    if ((validateEmail(email)) || originalText(email)) {
        ...
        return true;
    } else {
        ...
        return false;
    }
});