JQuery val() 返回空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10830008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 09:49:39  来源:igfitidea点击:

JQuery val() returns empty string

jquerystringreturn

提问by elector

This is very puzzling and I am not even sure what code should I post here. Yet the problem is very simple. I have a form in a JQuery modal dialog where I am doing some price calculations. Among others, I have a field Discount. Presented as:

这非常令人费解,我什至不确定我应该在这里发布什么代码。然而问题很简单。我在 JQuery 模态对话框中有一个表单,我正在做一些价格计算。其中,我有一个领域的折扣。呈现为:

<div class="editor-label">
    @Html.LabelFor(model => model.Discount)
</div>
<div class="editor-field">
    @Html.EditorFor(model => model.Discount, new { size = 10 })
</div>

I see in the Firebug debugger, DOM tab, that this field has a value of 20. And it is visible on the form, of course. But calling:

我在 Firebug 调试器的 DOM 选项卡中看到,该字段的值为 20。当然,它在表单上可见。但是调用:

 $('#Discount').val()

returns an empty string. And this is the only field that has this problem. Calling val() for other fields returns correct values. Even clicking submit and debugging the MVC action for the model submitted, I see that the value for Discount is there. It is just the JQuery function that returns an empty string and only for this field.

返回一个空字符串。这是唯一存在此问题的领域。为其他字段调用 val() 会返回正确的值。即使单击提交并调试提交的模型的 MVC 操作,我也看到 Discount 的值在那里。只是 JQuery 函数返回一个空字符串,并且只针对这个字段。

I know this is a bit of an abstract question, but has anyone ever had an experience like this?

我知道这是一个有点抽象的问题,但是有人有过这样的经历吗?

Thank you

谢谢

Generated HTML:

生成的 HTML:

<div class="editor-label">
  <label for="Discount">Popust (%)</label>
</div>
<div class="editor-field">
  <input id="Discount" class="text-box single-line valid" type="text" value="0" name="Discount" data-val-range-min="0" data-val-range-max="100" data-val-range="Vredonst mora biti izmedju 0 i 100" data-val-number="Polje Popust (%) mora biti broj" data-val="true" size="10">
</div>

While DOM tab in Firebug for this element shows value: "20"

Firebug 中此元素的 DOM 选项卡显示值:“20”

回答by Daren Schwenke

Check that id 'Discount' is unique.

检查 ID 'Discount' 是否唯一。

When putting multiple forms on a single page I leave the 'name' alone but always prefix the id of the 'form' element to the id of each 'value'.

当在一个页面上放置多个表单时,我会单独保留“名称”,但始终将“表单”元素的 id 放在每个“值”的 id 前面。

Saves me alot of headaches.

让我免去很多头痛。

回答by Daniel Morell

Daren's answer definitely hit the nail on the head.

达人的回答绝对是一针见血。

However, there are times when you want to return the value of multiple elements that have the same ID. (Note:IDs should be unique. Classes, on the other hand, don't need to be unique.)

但是,有时您希望返回具有相同 ID 的多个元素的值。(注意:ID 应该是唯一的。另一方面,类不需要是唯一的。)

To accomplish this you need to iterate with the .each()method. Most jQuery methods that return an object implicitly iterate. .val()returns a string, not an object. Because of that, it needs an explicit iterationloop.

为此,您需要使用该.each()方法进行迭代。大多数返回对象的 jQuery 方法都隐式地迭代.val()返回一个字符串,而不是一个对象。因此,它需要一个显式的迭代循环。

You can do use the .each()method like this.

你可以使用这样的.each()方法。

$('.discount').each(function(){
    $(this).val()
});

Please note that I changed the OP's ID to a class so that it is correct HTML.

请注意,我将 OP 的 ID 更改为一个类,以便它是正确的 HTML。