简体   繁体   English

jQuery无法从动态输入字段获取值

[英]jquery not getting values from dynamic input field

I have setup a hidden input field in Laravel 5.7 view as: 我在Laravel 5.7视图中将隐藏的输入字段设置为:

<input type="hidden" name="sbj_type_id" value="{{ $admin_option->last_sbj_type_id == $subject->id ? 'selected' : '' }}">

And I'm trying to get the value of this field in jquery as: 我正在尝试在jquery中获取此字段的值,如下所示:

$('input[name^="sbj_type_id"]').each(function() { alert($(this).val()); });

but empty alert is coming, after that I tried {{ dd($admin_option->last_sbj_type_id }} , then value displayed, but not getting it in jquery, first it was working with simple code as: 但是出现了空警报,之后我尝试了{{ dd($admin_option->last_sbj_type_id }} ,然后显示了值,但没有在jquery中获取它,首先它使用的是简单代码,如下所示:

var sbj_type_id = $('input[name="sbj_type_id"]').val();

Today it's not working at all. 今天,它根本不工作。

Your Jquery code is working so... 您的Jquery代码正在运行,因此...

In your ternary operator, if the condition is false you return an empty string. 在三元运算符中,如果条件为false,则返回一个空字符串。 Try to return something like : 尝试返回类似以下内容:

<input type="hidden" name="sbj_type_id" value="{{ $admin_option->last_sbj_type_id == $subject->id ? 'selected' : 'false' }}">

You will see where the problem is coming from. 您将看到问题的根源。

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

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