English 中文(简体)
j 质量: 是否等值
原标题:jQuery: Check whether val() has a specific value or not upon form submit
  • 时间:2010-05-31 15:10:04
  •  标签:
  • jquery
  • forms

I m trying to check whether val() has a specific value or not inside a form, if so deleting the contents / preventing the form being submitted depending on if both fields are filled in (stringOne and stringTwo). There s a default text for each input field, the first being "Namn, telefonnr, sökord" and the second "Område, plats, ort". If a user only fills in the first field the second must be cleared before the form string is passed on and vice versa. Like so -

// "This" refers to the form s submit button
if (($(this).siblings( input.stringOne ).val("Namn, telefonnr, sökord")) && ($(this).siblings( input.stringTwo ).val("Område, plats, 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, telefonnr, sökord")) {
        $(this).siblings( input.stringOne ).val() ==   ;
    }
    if ($(this).siblings( input.stringTwo ).val("Område, plats, ort")) {
        $(this).siblings( input.stringTwo ).val() ==   ;
    }
}

j 表格 1.2.6。

最佳回答

在 j中,当你在<代码>val(>内贴出价值时,请在<>ing<>>>>>上填写。

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

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

rel=“nofollow noreferer”> 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, telefonnr, sökord") && (!$strTwo.val() || $strTwo.val() == "Område, plats, ort" ) {
    return false; // If nothing s filled in, then do not submit
} else {
    // If either field is not filled in, clear it
    if ($strOne.val() == "Namn, telefonnr, sökord") {
        $strOne.val("");
    }
    if ($strTwo.val() == "Område, plats, ort" ) {
        $strTwo.val("");
    }
}
问题回答

我希望这将对某人有用:

本条

if ($( #my_text_field ).val() == "Clear Me") {
    $( #my_text_field ).val("");

能够写

$( #my_text_field[value="Clear Me"] ).val("");

甚至更好

$( #my_text_field[value="Clear Me"] ).val("").change();

改动后产生以下效应:在价值发生变化后,美元(#my_text_field[数值=“Clear Me”)与#my_text_field不相称。





相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签