views:

39

answers:

4

I have a form which contains 4 text fields when the button is clicked we should see that atleast one of the textfield should contain the value and the form should get submitted and the form also contains the Button which of input type too .I want only textbox field values

$("#mybutton").click(function(){

$(":input").each(function(){
  if($(this).value!=''){
    $("#myform").submit();

 }

});
});
+1  A: 

Try this out. I've replaced your :input selector with :text to only get textboxes. Also you were calling .value on the jquery object rather than using the .val() method.

$("#mybutton").click(function() {
    $(":text").each(function() {
        if($(this).val() !='') {
            $("#myform").submit();
            return false;
        }
    });
});
Wallace Breza
Like @joshperry's original answer, this could submit the form multiple times.
Doug Neiner
@Doug Neiner - Thanks, I've updated my answer to return false to break out of the each statement.
Wallace Breza
+1  A: 

Use the input:text selector to get all the textboxes.

$("input:text")
joshperry
This would submit the form multiple times :)
Nick Craver
Thanks, I guess his code was flawed. I was answering the question not doing a code review...
joshperry
You can just add a `return false` within your each iteration and jquery will automatically treat that the same as a `break` in a traditional loop.
Wallace Breza
Again, I was just regurgitating his code. So I've just removed everything that doesn't answer the specific question.
joshperry
+5  A: 

You can use :text for the selector, .filter() down to ones that have a value and check the .length, like this:

$("#mybutton").click(function(){
  if($(":text").filter(function(){ return this.value!=''; }).length)
    $("#myform").submit();
});

this checks if any aren't empty, and if that's the case, submits the form once.


And here's an alternative that checks on <form> submit, compliments of doug's comment:

$("#myform").submit(function(){
  if(!$(":text").filter(function(){ return this.value!=''; }).length)
    return false;
});
Nick Craver
I still would bind to `submit` instead of button click, but the filter is a really nice touch!
Doug Neiner
Nice update. I already +1'ed or or I would do it again :)
Doug Neiner
@nick:I want to submit only if there is atleast one field entered
Someone
@Someone - This would do that, for example the second one's checking for any that have a non-empty values, if there are any it keeps submitting `if(!1)` wouldn't `return false` for example, if none had a value, then `if(!0)` *would* `return false;`, stopping the submit from happening.
Nick Craver
+3  A: 

First, bind to form submit not button click. If a user hits enter/return in a field, it will submit your form without the checks:

$("form").submit(function (e) {
  var fields = $(this).find(':text'),
      cancel = true;

  fields.each(function (i, el) {
    if (el.value) {
      cancel = false;
      return false; // exit this loop
    }
  });

  if (cancel) {
    e.preventDefault(); // cancel submit since all fields are blank
  }      
});
Doug Neiner
+1 for the form submit event recommendation
joshperry