3 回答
Please use filtering of fields, since [value=""]
selector checks the default states of the inputs.
$('#form').on('submit', function() {
$(':input', this).filter(function() {
return this.value.length == 0;
}).remove();
});
DEMO: http://jsfiddle.net/bCskH/
UPDATE: In order not removing input fields you can simply disable them:
$('#form').on('submit', function() {
$(':input', this).filter(function() {
return this.value.length == 0;
}).prop('disabled', true);
});
The effect will be the same, however IMHO much nicer :)
You can use jquery selector and serialize()
to select only input that has value
here is the example
<form id="test">
<input name="field1" value="Test"><br/>
<input name="field2" value=""><br/>
<input name="field3" value="value"><br/>
<input id="btn" type="button" value="submit"/>
</form>
$('#btn').on('click',function() {
alert($('#test input[value != ""]').serialize());
});
also I doubt, .live()
will work in jquery 1.7 use .on()
instead. See this link and it says it has been deprecated
[value=".."]
accesses the attribute which is not synced with the current value. Besides that, you cannot specify a selector as context - so move #form
into the main selector and use .filter()
to restrict the matched elements to empty ones:
$('#form input').filter(function() {
return !$(this).val();
});