JQuery 语法,带有 .val(""); (返回数组)

发布于 2024-12-17 22:08:17 字数 1017 浏览 1 评论 0 原文

我对 Jquery 很陌生,所以我对如何执行下面描述的操作有点困惑...

我想要做什么:

  1. 搜索整个页面以获取输入包含“费率”一词和“数量”一词的文本框。

  2. 进行计算,求和(费率 * 数量)

  3. 将结果呈现给用户。

我所做的

  1. 因为行是动态添加的,所以元素名称中有 GUID,所以我搜索它们。

在我需要帮助的地方,

我可以使用 .val("") 命令提取值,但我对如何执行我需要的步骤的语法感到困惑。

这就是我所拥有的:

  $("#alertButton").live("click", function () {
                var rateValues = $('input[name*="Rate"]').val("");
                var qtyValues = $('input[name*="Qty"]').val(""); 

                           //how do i handle it here

            });

这是其中一个费率文本框的呈现方式,就像在 html 中一样(可能有 0 到多个文本框,因为它们是动态添加到页面中的)

<input type="text" value="0" style="width: 100%;" name="JobDetails[dd1c94e5-5e83-4a92-b5c4-0a5aea0eb5df].Rate" id="JobDetails___randomNumber___Rate" data-val-required="The Rate (Exl GST) field is required." data-val-number="The field Rate (Exl GST) must be a number." data-val="true">

I am quite new to Jquery, so I am a bit confused as to how I can perform what is described below...

What I am trying to do:

  1. Search through the entire page for input textboxes that have the word Rate and then again for Quantity.

  2. Do a calculation, Sum of (Rate * quantity)

  3. Present the result to the user.

What I have done

  1. Because the rows are added dynamically there are GUIDs in the elements name, so I search for them.

Where I need help

I can extract the value using the .val("") command, but I am confused the syntax as to how I could perform the steps i need to.

Here is what I have:

  $("#alertButton").live("click", function () {
                var rateValues = $('input[name*="Rate"]').val("");
                var qtyValues = $('input[name*="Qty"]').val(""); 

                           //how do i handle it here

            });

This is what the one of the rate textboxes is rendered like in html (there may be 0 to many of these, as they are dynamically added to the page)

<input type="text" value="0" style="width: 100%;" name="JobDetails[dd1c94e5-5e83-4a92-b5c4-0a5aea0eb5df].Rate" id="JobDetails___randomNumber___Rate" data-val-required="The Rate (Exl GST) field is required." data-val-number="The field Rate (Exl GST) must be a number." data-val="true">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

混吃等死 2024-12-24 22:08:17

当您将参数传递给“.val()”时(即使是空字符串),您设置的值。您可以使用“.val()”(无参数)获取值,但这仅获取第一个匹配元素的值。

总的来说,您可能想要的是使用“.map()”:

var rateValues = $('input[name*="Rate"]').map(function() {
  return this.value;
});
var qtyValues = $('input[name*="Qty"]').map(function() {
  return this.value;
});

这将为您提供包含值和数量的数组。如果您还想要“id”或“name”,您可以返回其他内容:

var rateValues = $('input[name*="Rate"]').map(function() {
  return { value: this.value, name: this.name };
});

然后您将拥有一个对象数组。

When you pass an argument to ".val()" — even the empty string — you set the value of the <input>. You get the value with ".val()" (no parameters), but that only gets the value of the first matched element.

What you may want, overall, is to use ".map()":

var rateValues = $('input[name*="Rate"]').map(function() {
  return this.value;
});
var qtyValues = $('input[name*="Qty"]').map(function() {
  return this.value;
});

That'll give you arrays with the values and quantities. You could return something else if you also wanted the "id" or "name":

var rateValues = $('input[name*="Rate"]').map(function() {
  return { value: this.value, name: this.name };
});

Then you'd have an array of objects.

青萝楚歌 2024-12-24 22:08:17

要检索值,您必须使用 .val() (无引号)。您实际上将这些字段的值设置为空字符串。

由于您说这些字段可能不止一对,因此您必须实际循环搜索结果,检索每对的值,然后进行计算。现在,您将该 val 应用于所有匹配的输入。

var rateValues = $('input[name*="Rate"]').val();
var qtyValues = $('input[name*="Qty"]').val(); 
$(rateValues).each(function(i, val) {
     // val is rateValues[i]
     qty = qtyValues[i];
     ... do your calculations
});

To retrieve a value, you have to use .val() (no quotes). You're actually SETTING those fields' values to be an empty string.

Since you say there could be more than one pair of those fields, you'll have to actually loop over the search results, retrieve each pair's value, then calculate. Right now, you're applying that val to ALL matching inputs.

var rateValues = $('input[name*="Rate"]').val();
var qtyValues = $('input[name*="Qty"]').val(); 
$(rateValues).each(function(i, val) {
     // val is rateValues[i]
     qty = qtyValues[i];
     ... do your calculations
});
温馨耳语 2024-12-24 22:08:17
$("#alertButton").live("click", function () {
   var rateValues=new Array();
    var qtyValues=new Array();
        $('input[name*="Rate"]').each(function(){
rateValues.push($(this).val());
   });
        $('input[name*="Qty"]').each(function(){
qtyValues.push($(this).val());
   });

 });

应该给你一系列的费率和数量的数组

$("#alertButton").live("click", function () {
   var rateValues=new Array();
    var qtyValues=new Array();
        $('input[name*="Rate"]').each(function(){
rateValues.push($(this).val());
   });
        $('input[name*="Qty"]').each(function(){
qtyValues.push($(this).val());
   });

 });

should get you array of rates and array of qty

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文