1
4

3 回答 3

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 :)

DEMO: http://jsfiddle.net/bCskH/1/

于 2012-05-05T13:41:28.407 回答
2

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

于 2012-05-05T13:47:47.877 回答
1

[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();
});
于 2012-05-05T13:38:42.160 回答