HTML: Checkbox default value

后端 未结 3 523
执念已碎
执念已碎 2021-01-01 10:38

When I submit a HTML form with a checked checkbox that doesn\'t have an explicitly defined value, Chrome sends on as a value for that field.

<
3条回答
  •  伪装坚强ぢ
    2021-01-01 11:09

    Browsers will send the value of a checkbox (in the POST data) only if it is checked. A check to see if a value (any value) for a particular checkbox is present in the POST data is all you need.

    i.e.

    // no need to check against 'on', 'true', '1' etc..
    if(post data contains a value for checkbox1) {
        // checkbox 1 is checked
    }
    else {
        // not checked
    }
    

提交回复
热议问题