javascript - jQuery val() 在输入字段中输入正确的数字,但标签仍然不显示任何值

标签 javascript jquery forms laravel-4

我试图让 jQuery 根据订单上的输入字段更新字段。我使用 jQuery 的 val() 来设置值,但是当我尝试使用 Laravel 4 的 Validator::验证订单表单时,它说该字段为空。当我检查该字段时,我发现尽管输入字段中有正确的数字,但仍然没有实际分配给该字段的值。如何使用 jQuery 的 val() 设置 Laravel 4 的 Validator::能够看到并识别为合法输入的值?如果有人能解释我哪里出错了或者告诉我如何正确地做到这一点,我将非常感激!非常感谢!

这是相关的 jQ 片段,当页面加载 num = 1 时,因为总会有至少 1 个页面:

var num = 1;
$('#number_of_pages').val(num);

这是代码片段的最后一位,用于在用户单击“AddPage”按钮时更新页数:

/*Update number of pages*/
var updated_number_of_pages = $('.inputPage').length;
$('#number_of_pages').val(updated_number_of_pages);

当他们单击“删除页面”按钮时:

/*Updated Number of pages*/
var updated_number = $('.inputPage').length;
$('#number_of_pages').val(updated_pages);

这是 View :

{{ Form::text('number_of_pages', null, array('id' => 'number_of_pages')) }}
    @if($errors->has('number_of_pages'))
        {{ $errors->first('number_of_pages') }}
    @endif

以及 Controller 的相关部分:

$validator = Validator::make(Input::all(),
               array(
                'order_name'      => 'required',
                'phone'           => 'required|min:10|numeric',
                'email'           => 'required',
                'st_address'      => 'required',
                'post_date'       => 'required',
                'number_of_pages' => 'required',
               )            
);

最佳答案

虽然我对这个问题有点困惑,但您可以使用 attr 在源中设置属性:

$('#number_of_pages').attr('value', updated_number_of_pages);

因此,您可以看到源代码中的更改,val() 在内存中设置属性。

更新:

还要确保您的表单是使用 Form::open() 打开的,并且 text 字段位于表单内,如下所示:

{{ Form::open(...) }}
    {{ Form::input('number_of_pages') }}
    {{-- Rest of the code --}}
{{ Form::close() }}

关于javascript - jQuery val() 在输入字段中输入正确的数字,但标签仍然不显示任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22674572/

相关文章:

javascript - 如何找到div下方页面部分的高度?

JavaScript - 在主体上加载下一个数组元素

javascript - 当 html 元素有 contenteditable 时立即显示下划线 ="true"spellcheck ="true"

jquery - 在哪里存储额外信息?

javascript - 未捕获的类型错误 : Cannot call method 'menuAim' of null

PHP 表单未发布到 MySQL 数据库

javascript - 在 Userpro 插件中获取新注册用户的用户 ID

asp.net - 如何使用 jQuery 和 asp.net 进行 onclientclick 回发

c# - 在时间段内激活

html - 提交的表单不发布数据(提交=>提交除外)