简体   繁体   English

如何发布有条件的已禁用/只读输入字段?

[英]How to POST input field that are disabled/readonly with condition?

I am trying to make my input field disabled or readonly if a button is not yet pressed but I am having problems with posting/submitting it to the database after I added a condition which makes it disabled or readonly 我试图使输入字段处于禁用状态或只读状态(如果尚未按下按钮),但是在添加使其变为禁用状态或只读状态后,我在将其发布/提交到数据库时遇到问题

    <form action="{{ route('amber.timestone.home.start', $task->id) }}" method="POST" class="align-center">
                                {{csrf_field()}}
                            <td>
                                <input class="my-2" type="text" name="ref" value="{{ $task->ref }}"
                                    @if(empty($task->start))
                                        readonly
                                    @endif
                                >


public function startTask($id)
    {
        $user = Task::find($id);
        $user->start = Carbon::now();
        $user->save();
        return back();
    }
public function endTask($id, Request $request)
    {
        $user = Task::find($id);
        $user->end = Carbon::now();
        $start = Carbon::parse($user->start);
        $end = Carbon::parse($user->end);
$user->ref = request('ref');
        $user->remarks = request('remarks');
        $user->campaign = request('campaign');
        $user->type = request('type');
            $hours = $end->diffInHours($start);
            $minutes = $end->diffInMinutes($start);
            $seconds = $end->diffInSeconds($start);
            $user->duration = $hours . ':' . $minutes . ':' . $seconds;
            $user->update();
            return back();
        }

I tried adding hidden with the same names as for those who are with readonly condition still not working 我尝试添加与那些具有只读条件的名称相同的隐藏名称仍然无法正常工作

Disabled fields are not posted with the requests however you can add readonly attribute. 禁用字段不随请求一起发布,但是您可以添加只读属性。

One other thing you can do is that disabling the field for the UI purpose and add a hidden input below with the same name and the value. 您可以做的另一件事是,出于UI目的禁用字段,并在下面添加具有相同名称和值的隐藏输入。

HTML is a markup language, it highlights the data semantic, not the way the data is seen (eg using <strong> to make a text bold is a totally wrong way). HTML是一种标记语言,它突出显示数据的语义,而不是显示数据的方式(例如,使用<strong>使文本加粗是完全错误的方式)。 So you should use a CSS-customized text container( <div> or <span> mostly) to encapsulate it and being btw read-only. 因此,您应该使用CSS定制的文本容器(主要是<div><span> )将其封装并保持只读。 Then, just like Adam abdul shakoor suggested, you may use an hidden input with the same value. 然后,就像Adam abdul shakoor所建议的那样,您可以使用具有相同值的隐藏输入。 You should also consider using VueJS (already packaged within Laravel), its data-driven approach makes this kind of things trivial to keep container's and input's contents synchronized. 您还应该考虑使用VueJS(已经打包在Laravel中),它的数据驱动方法使这种事情变得微不足道,以保持容器和输入的内容同步。

What do you think? 你怎么看? If you have a value that needs to be sent back, place it in the hidden input field. 如果您有需要返回的值,请将其放在隐藏的输入字段中。 So you could create a fake field using css, maybe. 因此,也许可以使用CSS创建一个伪字段。 But if you do not want to send any data, just to show it, you may simply not include the hidden input field with the value. 但是,如果您不想发送任何数据,只是为了显示它,您可能根本就不会在隐藏输入字段中包含该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM