在同一列中保存下拉列表或文本输入值(使用Laravel 5.5)

时间:2017-10-01 17:55:37

标签: javascript php jquery forms laravel

之情况: 如果用户选择"其他"状态下拉选项将出现,然后用户将手动进入状态....

我的问题是,如果用户在文本输入中写入状态名称,该值必须进入Drop Down的值所在的同一列。简而言之,我的数据库中有一列状态,我想保存用户给出的状态名称是来自下拉菜单还是文本输入....

状态输入和下拉.....

<select class="states" name="state" size="1" required>
       @include('auth.partials._states')
</select>

 <tr class="hidden state-input">
    <td align="right">&nbsp;&nbsp;</td>
    <td><input type="text" name="state" size="30" maxlength="100" value=""></td>
 </tr>

这是我用于隐藏和显示文本输入的JQuery

$('.states').change(function(){
            var selected_option = $('.states').val();
            if (selected_option === 'other') {
                $('.state-input').removeClass('hidden');
            }
            else
            {
                $('.state-input').addClass('hidden');
            }
        });

这是我的创建新用户功能,其中状态与其他字段一起

protected function create(array $data)
    {
        if(Input::hasFile('image'))
        {
            $email = Input::get('email');
            $image = Input::file('image');
            $image_extension = $image->guessClientExtension();
            $image_path = $image->storePubliclyAs('public/uploads/profile_images/images', $email . ".{$image_extension}");
        }


        $user = new User([
            'firstname' => $data['firstname'],
            'lastname' => $data['lastname'],
            'company' => $data['company'],
            'classification' => $data['classification'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'image_url' => $image_path,
            'skills' => $data['skills'],
            'interests' => $data['interests'],
            'about' => $data['about'],
            'website' => $data['website'],
            'address' => $data['address'],
            'phone' => $data['phone'],
            'country' => $data['country'],
            'zipcode' => $data['zipcode'],
            'city' => $data['city'],
            'state' => $data['state'],
        ]);

        $user->save();
return $user;
}

1 个答案:

答案 0 :(得分:0)

$('#submit_button').click(function(e) {
    e.preventDefault();
    var newValue = $('custom_state_text_box_selector').val(); // Modify me
    $('.states').val(newValue);
    $('your_form_selector').submit();
});

此外,无需设置文本字段的name即可进入自定义状态。而且,您提供与name下拉列表相同的select。去掉它。 要么 您也可以在服务器端执行此操作。提供文本框名称&#34; custom_state&#34;然后

'state' => $data['state'] == "other" ? $data['custom_state'] : $data['state']