jQuery:在表单提交时检查val()是否具有特定值

我正在尝试检查val()在表单内是否具有特定值,如果这样,则删除内容/防止表单提交取决于两个字段是否都填写(stringOne和stringTwo).每个输入字段都有一个默认文本,第一个是“ Namn,telefonnr,sökord”,第二个是“Område,plats,ort”.如果用户仅填写第一个字段,则必须先清除第二个字段,然后再传递表单字符串,反之亦然.
像这样-

// "This" refers to the form's submit button
if (($(this).siblings('input.stringOne').val("Namn,sökord")) && ($(this).siblings('input.stringTwo').val("Område,ort"))) {
    return false; // If nothing's filled in,then do not submit
} else {
    // If either field is not filled in,clear it
    if ($(this).siblings('input.stringOne').val("Namn,sökord")) {
        $(this).siblings('input.stringOne').val() == '';
    }
    if ($(this).siblings('input.stringTwo').val("Område,ort")) {
        $(this).siblings('input.stringTwo').val() == '';
    }
}

jQuery版本1.2.6.

最佳答案
在jQuery中,当您在val()中放置一个值时,就是在设置该值.

$('myElement').val()  // returns the value

$('myElement').val('some string')  // sets the value

.val()-http://api.jquery.com/val/

var $strOne = $(this).siblings('input.stringOne');
var $strTwo = $(this).siblings('input.stringTwo');

// "This" refers to the form's submit button
if ( (!$strOne.val() || $strOne.val() == "Namn,sökord") && (!$strTwo.val() || $strTwo.val() == "Område,ort" ) {
    return false; // If nothing's filled in,clear it
    if ($strOne.val() == "Namn,sökord") {
        $strOne.val("");
    }
    if ($strTwo.val() == "Område,ort" ) {
        $strTwo.val("");
    }
}

相关文章

1.第一步 设置响应头 header('Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...