在选择器中插入函数参数

Jquery insert function params into selector

本文关键字:函数 参数 插入 选择器      更新时间:2023-09-26

我有一个Jquery函数,可以帮助验证1个对象。我需要展开它,使函数可以运行在3个不同的对象上。我试图定义一个函数,它接受一个参数(引号)来在函数中插入适当的对象。这是我的代码。我做错了什么?我假设我没有正确的选择器,因为如果我把它放在。

原函数:

  var depends = function() {
      var selectorD = $("input[name^='lead[quote_diamonds_attributes]'], select[name^='lead[quote_diamonds_attributes]']");
      var vals = '';
      selectorD.not(':eq(0)').each(function () {
          vals += $(this).val();
      });
      return vals.length > 0;
  };

函数我试图创建,允许我在其他对象上使用它。

 var depends = function(whichquote) {
      var selectorD = $("input[name^='lead[+ whichquote +]'], select[name^='lead[+ whichquote +]']");**
      var vals = '';
      selectorD.not(':eq(0)').each(function () {
          vals += $(this).val();
      });
      return vals.length > 0;
  };

我认为问题是我在var selectorD中拼接,但似乎无法获得语法正确。

您的选择器实际上没有输入whichquote,因为字符串连接不正确。

var selectorD = $("input[name^='lead[" + whichquote + "]'], select[name^='lead[" + whichquote +"]']");