dongpo5264 2014-07-19 19:09
浏览 56
已采纳

冲突中的复选框和默认值[重复]

This question already has an answer here:

I have noticed a problem when using checkboxes.

I load my form with default values like this:

//Defaults for this template
$default['video'] = '';
$default['height'] = '';
$default['width'] = '';
$default['codec'] = '';
$default['time'] = '0';
$default['audio'] = '';
$default['video_id'] = '';
$default['filename_machine'] = '';
//in this template we also use x pos and y pos, but these depend on screen layout ie. not stored on entity.

$data = (array)$data + (array)$default;

if ($data['audio']=='on') {
    $audio = 'checked';
}
else {
    $audio = '';
}

<td>
    Spela upp ljud? <input type='checkbox' name='data_audio' $audio />
</td>

Now this works very well if the default is to not have a box checked, but if the default is to have a box checked, then that box will come up as checked even when the user has unchecked the box, saved the form and then gone back to edit the entity.

I think one way would be to not use checkboxes as the actual form field that gets saved, but use a hidden field behind, and only use the checkbox to set a value "on" or "off" in that field, or something like that. But a lot of extra work.

These specific form fields that I have defaults for in this way are saved by jQuery serialize so I don't have a good way of stepping through and manipulating fields based on type etc (like "if checkbox and not checked, set value to 'uncheck'" or something like that..), otherwise I guess that would be one way to go.

Is there a smarter trick I haven't thought of?


UPDATE

This is how I save all form fields prepended with data_

In the view I first do this before submission:

var myChunk = $( '#contentForm' ).serialize();
$( "#serialized" ).val( myChunk );

Then, in the php receiving the form:

    //Get the custom data fields
    $serialized = $this->unserializeForm( $request->request->get('serialized') );
    $jsonArray = $this->getDataFields($serialized,true); //init the array that will become the json
    $dataArray = $this->getDataFields($serialized); //init the array that will becom the one-dimensional array (currently in use)

Here is what the getDataFields look like, perhaps I could add some extra filtering in here for checkboxfields...

private function getDataFields($serialized,$toJson=false) {

    $myArray = array();

    foreach($serialized as $i) {

        $label = $i[0];
        $value = $i[1];

        //find only the data fields (prepended with "data_", skip rest of form)
        if(substr($label,0,5) == 'data_') {

            //discard the "data_" part of the label
            $label = substr($label,5);

            if($toJson == true) {
                //we're preparing data to be stored as json so we use a multidimensional for better overview.

                //see if there is a subgroup to the label (ie. data_BACKGROUND_color or such)
                if (strpos($label,'_') !== false) {
                    //echo 'har undergrupp <br />';
                    $parts = explode("_", $label); //split label
                    $group = $parts[0];
                    $label = $parts[1];
                    $myArray[$group][$label] = $value; //organize into group and label
                }
                else {
                    //echo 'har inte undergrupp <br />';
                    $myArray[$label] = $value; 
                }

            }
            else {
                //we're storing data in a plain array
                $myArray[$label] = $value; 
            }
        }
    }

    return $myArray;
}
</div>
  • 写回答

1条回答 默认 最新

  • dpkrbe395930 2014-07-22 12:29
    关注

    This is how I solved it in my case.

    I couldn't use the nifty solution to this thread how can I override jquery's .serialize to include unchecked checkboxes suggested by Robin Naben because I had some form fields in there that were handled by a framework form processor, and others that I wanted to serialize.

    So I went with a custom loop instead, making an exception for that field that gets processed and validated through the framework.

    $("input[type=checkbox]").each(function () {
        if ( ($(this).attr("id") != 'biztv_contentmanagementbundle_contenttype_draft') && (this.checked == 0) ) { //loop through all unchecked checkboxes except the draft one ("UTKAST").
            $(this).attr('value', 'false'); //explicitly set false as the value for all unchecked checkboxes
            $(this).prop('checked', true); //This is necessary since otherwise it won't be included in the submission at all
        }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!