【问题标题】:Noob jQuery "$" function return questionNoob jQuery "$" 函数返回问题
【发布时间】:2011-03-15 11:13:48
【问题描述】:

HTML:

<input type="text" id="priceperperson1" name="priceperperson1" />
<input type="text" name="personsvalue1" class="countme" readonly="readonly" />

JS:

jQuery(document).ready(function($) {
$('div.pricerow input.countme').each(function(){ 
var id = this.name.substr(this.name.length-1);
alert($('input#priceperperson'+id));
this.value = parseInt($('priceperperson'+id).value) * parseInt($('countpersons'+id).value); 
});
});

尽可能缩短。我所有的警报都是“对象”......值是NaN。我试图在 id 上“parseInt”。我试过了:

$('[name=priceperperson'+id+']');
$('priceperperson'+id);

我做错了什么?

【问题讨论】:

  • 如果添加以下行会得到什么:alert('input#priceperperson'+id); ?您确定字符串正在按您希望的方式形成吗?
  • 您的代码可能包含错误! id = lenght 1 OK 如果 id 长度 > 1 ??可能的例外

标签: javascript jquery html object


【解决方案1】:

当您执行 $(..) 时,您正在检索 jQuery 对象

要获取值(字符串),请使用.val() 方法。

所以

alert( $('input#priceperperson'+id).val() );

【讨论】:

    【解决方案2】:

    您应该将$ 放在函数定义中。

    我猜它会导致 $ 变量在函数中被重新定义——并且不再指向 jQuery 的 $() 函数。


    我正在考虑这个:

    jQuery(document).ready(function($) {
    

    尝试使用,而不是:

    jQuery(document).ready(function() {
    

    【讨论】:

      【解决方案3】:

      当你在循环 jquery 对象时,我相信你必须使用:

      $(this).attr('name'); 而不是 this.name

      另外,要从对象中调用值,您必须使用 $.val() 或 $.attr('attributename');

      // Shortcut for doc ready
      $(function() 
      { 
       // Loop through values
       var id = $(this).attr('name');
      
       alert($('input#priceperperson' + id).val());
      });
      

      【讨论】:

        【解决方案4】:

        您是否在寻找.val()

        this.val(parseInt($('priceperperson'+id).val()));
        

        【讨论】:

          【解决方案5】:

          我所有的警报都是“对象”...值 是 NaN。我试过“parseInt”

          尝试给parseInt一个基础:

          parseInt(variable, 10);
          

          【讨论】:

            【解决方案6】:

            有一些错误...要获取 jQuery 对象的值,您必须使用 .val() 方法,而不是 .value

            然后,parseInt() 需要基数作为第二个参数。比如:

            ...
            this.value = parseInt($('priceperperson'+id).val(), 10) * parseInt($('countpersons'+id).val(), 10); 
            ...
            

            【讨论】:

              【解决方案7】:

              您的代码可能包含错误

              您的代码 id = 长度 1 OK 如果 id 长度 > 1 ??可能的异常

              priceperperson1 到 priceperperson_1

              # option # proposal
              
              <input type="text" id="priceperperson_1" name="priceperperson_1" /> // => use "_"
              <input type="text" name="personsvalue_1" class="countme" readonly="readonly" />
              
              
              jQuery(document).ready(function($) {
                  $('div.pricerow input.countme').each(function(){ 
                      var id = this.name.split("_")[1]; // => get array value 
                      this.value = parseInt($('[name=priceperperson_'+id+']').value()) *parseInt($('[name=countpersons='+id+']').value()); 
                  });
              });
              

              【讨论】:

              • 不可能 - 只有 4 个元素。
              猜你喜欢
              • 2010-11-18
              • 1970-01-01
              • 1970-01-01
              • 2013-05-08
              • 2021-09-26
              • 1970-01-01
              • 2013-03-02
              • 2016-01-08
              • 2022-01-13
              相关资源
              最近更新 更多