私信  •  关注

Rory McCrossan

Rory McCrossan 最近创建的主题
Rory McCrossan 最近回复了
3 年前
回复了 Rory McCrossan 创建的主题 » 在jquery的intl country code下拉列表中添加国家代码

要做你需要的事,打电话给 setCountry 选择 intlTelInput 并提供 country-code 所选数据库中的数据属性值 option :

$(function() {
  $("#country").change(function() {
    let countryCode = $(this).find('option:selected').data('country-code');
    let value = "+" + $(this).val();
    $('#txtPhone').val(value).intlTelInput("setCountry", countryCode);
  });
  
  var code = "+977";
  $('#txtPhone').val(code).intlTelInput();
});
<div class="container-fluid">
  <div class="row">
    <h1>Select Country</h1>
  </div>
  <div class="row">
    <div class="col-md-12 form-group">
      <div class="col-md-6 form-group hire-input-country_box position-relative">
        <select name="country" class="form-control" id="country">
          <option data-country-code="DZ" value="213">Algeria</option>
          <option data-country-code="AD" value="376">Andorra</option>
          <option data-country-code="AO" value="244">Angola</option>
          <option data-country-code="AI" value="1264">Anguilla</option>
          <option data-country-code="AG" value="1268">Antigua &amp; Barbuda
          </option>
        </select>
      </div>
    </div>
    <div class="col-md-12 form-group">
      <input type="tel" id="txtPhone" class="txtbox" />
    </div>
  </div>
</div>

<script type="text/javascript" src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/16.0.8/js/intlTelInput-jquery.min.js"></script>

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/16.0.8/css/intlTelInput.css" />
3 年前
回复了 Rory McCrossan 创建的主题 » 使用jquery从表行请求文本返回“”

你的代码中有两个问题。首先,你使用了箭头函数 this 关键字将指向外部上下文,而不是 tr 迭代中的元素。要修复此问题,请使用匿名函数,或接收 tr 作为arrow函数的参数。我在下面的例子中做了后者。

第二个问题是你正在使用 filter() 作为迭代器,这是不正确的。 过滤器() 应用于减少jQuery对象中的一组元素。在这种情况下,如果要循环,只需使用 each() 相反

jQuery($ => {
  $('#test tr').each((i, tr) => {
    console.log($(tr).text().trim());
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<table>
  <tbody id="test">
    <tr>
      <td>example</td>
      <td>1</td>
    </tr>
    <tr>
      <td>example</td>
      <td>2</td>
    </tr>
    <tr>
      <td>example</td>
      <td>3</td>
    </tr>
    <tr>
      <td>example</td>
      <td>4</td>
    </tr>
  </tbody>
</table>
3 年前
回复了 Rory McCrossan 创建的主题 » Jquery仅在重定向后向下滚动

为了实现这一点,您可以在URL中添加一个片段,当主页下次加载时可以检测到该片段:

在产品页面中添加 # 链接到URL的片段:

<a href="{{ path('home_index') }}#foo" class="return-ad">
  <img src="{{ app.request.getBaseURL()/arrow-white-left.png">
</a>

然后在主页中,检测该片段并执行动画:

jQuery($ => {
  if (window.location.hash && window.location.hash.substring(1) === 'foo') {
    $('html,body').animate({
      scrollTop: $(".align-items-stretch").offset().top
    }, 1000);  
  }
});

还要注意 ready() 方法应在 document ,而不是DOM中的元素。我修改了上面的例子,使用了a的缩写形式 document.ready 事件处理程序

13 年前
回复了 Rory McCrossan 创建的主题 » 使用jQuery在数据库上保存变量和表单输入。post()

serialize() 有效地将表单值转换为有效的查询字符串,因此您只需将以下内容附加到字符串:

$.ajax({
    type : 'POST',
    url : 'url',
    data : $('#form').serialize() + "&par1=1&par2=2&par3=232"
}
5 年前
回复了 Rory McCrossan 创建的主题 » jQuery不是在第一次单击时工作,而是在每隔一次单击时工作

问题是因为没有 data-clicks clicks false ,所以它试图隐藏内容。需要反转此标志,逻辑才能按预期工作,因此第一次单击时会显示元素:

var clicks = !$(this).data('clicks');    

以下是完整的工作片段:

jQuery(document).ready(function() {
  $("#menu-options").on('click', '#show-meat', function() {
    var clicks = !$(this).data('clicks');    
    if (clicks) {
      $(".menu-options-choice-veget, .menu-options-choice-vegan").hide("slow");
      $(".menu-options-choice-meat").animate({
        'margin-right': '-800px'
      }, 'slow');
    } else {
      $(".menu-options-choice-veget, .menu-options-choice-vegan").show("slow");
      $(".menu-options-choice-meat").animate({
        'margin-right': '0px'
      });
    }
    $(this).data("clicks", clicks);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="menu" id="menu-options">
  <ul>
    <li><button type="button" class="menu-otpions-button" id="show-meat">Meat</button></li>
    <li><button type="button" class="menu-otpions-button" id="show-veg">Vegetarian</button></li>
    <li><button type="button" class="menu-otpions-button" id="show-vegan">Vegan</button></li>
  </ul>
</div>
<div class="menu-options-choice-veget">Vegetarian</div>
<div class="menu-options-choice-vegan">Vegan</div>
<div class="menu-options-choice-meat">Meat</div>
5 年前
回复了 Rory McCrossan 创建的主题 » 从同一父div复制链接-添加到同一div jquery中的另一个子项

你在绕着 .ecommMenuItems 元素,该元素高于每个菜单的分组级别。相反,你需要在 .ecommMenuItem

$(".ecommMenuItem").each(...

还要注意,这样可以避免设置内联 display: none li 使用的元素 :nth-child()

$(".ecommMenuItem ul").append('<li><a href="#" class="view-all">View All</a></li>');

$(".ecommMenuItem").each(function() {
  var divtext = $(".category-link", this).attr("href");
  $(".view-all", this).attr("href", divtext);
});
.ecommMenuItem li:nth-child(n+5):not(:last-child) {
  display: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<div class="ecommMenuItems">
  <div class="ecommMenuItem">
    <a class="category-link" href="/subgroup/bga-stencil/" title="BGA Stencil"><i class="category-icon">
      <img src="/ebuyerfix-com/_img/img/placeholder.jpg"></i>
      <span class="category-title" style="height: 15px;">BGA Stencil</span>
    </a>
    <ul>
      <li><a id="catId8468" href="/_shop/ur-selected-stencil/" title="UR Selected Stencil">UR Selected Stencil</a></li>
      <li><a id="catId8469" href="/_shop/laser-tech-stencil/" title="Laser Tech Stencil">Laser Tech Stencil</a></li>
      <li><a id="catId8470" href="/_shop/3d-stencil/" title="3D Stencil">3D Stencil</a></li>
      <li><a id="catId8471" href="/_shop/black-stencil/" title="Black Stencil">Black Stencil</a></li>
      <li><a id="catId8472" href="/_shop/magnetic-stencil/" title="Magnetic Stencil">Magnetic Stencil</a></li>
      <li><a id="catId8473" href="/_shop/macbook-stencil/" title="MacBook Stencil">MacBook Stencil</a></li>
    </ul>
  </div>
  <div class="ecommMenuItem">
    <a class="category-link" href="/subgroup/hand-tools/" title="Hand Tools"><i class="category-icon">
      <img src="/ebuyerfix-com/_img/img/placeholder.jpg"></i>
      <span class="category-title" style="height: 15px;">Hand Tools</span>
    </a>
    <ul>
      <li><a id="catId8449" href="/_shop/screwdrivers/" title="Screwdrivers">Screwdrivers</a></li>
      <li><a id="catId8450" href="/_shop/tweezers/" title="Tweezers">Tweezers</a></li>
      <li><a id="catId8451" href="/_shop/tool-kit-set/" title="Tool Kit Set">Tool Kit Set</a></li>
      <li><a id="catId8452" href="/_shop/worksurface-mats/" title="Worksurface Mats">Worksurface Mats</a></li>
      <li><a id="catId8453" href="/_shop/prying--cutting/" title="Prying &amp; Cutting">Prying &amp; Cutting</a></li>
      <li><a id="catId8454" href="/_shop/gripping--holding/" title="Gripping &amp; Holding">Gripping &amp; Holding</a></li>
      <li><a id="catId8455" href="/_shop/crimping--heating/" title="Crimping &amp; Heating">Crimping &amp; Heating</a></li>
      <li><a id="catId8456" href="/_shop/cleaning-tools/" title="Cleaning Tools">Cleaning Tools</a></li>
      <li><a id="catId8457" href="/_shop/sim-card-tools/" title="SIM Card Tools">SIM Card Tools</a></li>
      <li><a id="catId8458" href="/_shop/fume-extractor/" title="Fume Extractor">Fume Extractor</a></li>
      <li><a id="catId8459" href="/_shop/labour-protection/" title="Labour Protection">Labour Protection</a></li>
      <li><a id="catId8460" href="/_shop/static-personnel-grounding/" title="Static Personnel Grounding">Static Personnel Grounding</a></li>
    </ul>
  </div>
</div>
6 年前
回复了 Rory McCrossan 创建的主题 » 如何停止使用jquery播放音频?

不能通过jQuery对象访问媒体控件。因此,您需要对元素对象本身调用与音频相关的方法。

使用 play() pause() 很明显,但是要将时间索引设置回文件的开头,需要使用 currentTime :

var audio = $('audio')[0]; // alternative: $('audio').get(0);

audio.pause();
audio.currentTime = 0;
audio.play();
5 年前
回复了 Rory McCrossan 创建的主题 » 在jquery中哪个更快:$(“selector”)或selector作为对象变量[副本]

如果要多次重新选择同一个元素,那么将jQuery对象存储在一个变量中总是要快得多,因为它消除了从DOM读取数据的需要,而DOM读取数据的速度相对要慢得多。

5 年前
回复了 Rory McCrossan 创建的主题 » jquery addClass不会立即生效

你的代码有几个问题。首先在“模拟”AJAX请求中使用循环这是一个同步操作,也是阻止更新ui的问题的原因;循环正在停止执行任何其他操作(即dom中元素的更新)。因此,它似乎只在循环完成后更新。

其次,一旦在 loadData() 函数将遇到同步性问题,因为您希望布尔值返回值指示ajax请求是否工作。这在异步逻辑中是不可能的因此,你需要与承诺和/或回调一起工作。

要做到这一点,你需要从 $.ajax() 加载数据() 功能然后你可以使用 done() fail() 方法根据请求的结果更新DOM应该是这样的:

$('#btnLoad').click(function() {
  showMsg('Loading, please wait ...', 'info');
  loadData().done(function() {
   showMsg('Loaded', 'info');
  }).fail(function() {
    showMsg('Error', 'warn');
  });
});

function loadData() {
  return $.ajax({
    url: '/yourpage',
    data: {
      foo: 'bar'
    }
  });
} 

下面是一个工作示例,通过在3秒后手动解析延迟对象来模拟ajax请求。由于正确使用了异步逻辑,您可以看到加载消息现在已正确显示。

$(document).ready(function() {
  function showMsg(msg, type) {
    $('#msgBox')
      .removeClass("ais-card-message-info ais-card-message-warn")
      .addClass("ais-card-message-" + type)
      .text(msg)
      .show();
  }
  
  $('#btnLoad').click(function() {
    showMsg('Loading, please wait ...', 'info');
    loadData().done(function() {
      showMsg('Loaded', 'info');
    }).fail(function() {
      showMsg('Error', 'warn');
    });
  });

  function loadData() {
    // mock AJAX request, just for this demo
    var deferred = jQuery.Deferred();
    setTimeout(function() {
      deferred.resolve();
    }, 3000);
    return deferred;
  }
});
.ais-card-message {
  padding: 6px;
  margin-top: 6px;
  margin-bottom: 6px;
  text-align: left;
}

.ais-card-message-info {
  background-color: lightskyblue;
}

.ais-card-message-warn {
  background-color: lightpink;
}
<div id="title">
  <p>Message "Loading, please wait ..." should appear below when button clicked.&nbsp; Followed by either "Error" or "Loaded" after a short delay.</p>
</div>
<div id="data">Data will be loaded here</div>
<div id="msgBox" class="ais-card-message">Messages should display here</div>
<div><button id="btnLoad">Load</button></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

处理日期格式时,始终转换为日期对象,然后从该对象的各种方法中输出所需的格式。围绕一个字符串的黑客是非常脆弱的,只是要求头痛。

考虑到这一点,您可以使用jquery的 text() 方法在每个 .data 元素并以所需格式返回日期,如下所示:

$(function() {
  $('p.data').text(function(i, t) {
    var d = new Date(t);
    return ('00' + d.getDate()).slice(-2) + '/' + ('00' + (d.getMonth() + 1)).slice(-2) + '/' + d.getFullYear();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="data">2019-10-30 10:20:10</p>
<p class="data">2018-01-20 10:20:10</p>
<p class="data">2012-03-10 10:20:10</p>
5 年前
回复了 Rory McCrossan 创建的主题 » 如何在jquery中进行for循环求和?

这里有一些问题。首先你应该打电话 get() 使用时 map() 只获取返回的值数组,而不是整个jquery对象集合。

第二,你在第一盘的时候把总数翻了一番 res 到数组中的值,然后重新定义 物件 再加上它的价值…不知为什么。你也在毫无理由地循环两次;一次创建数组,然后再次通过数组。只循环一次。

最后,在处理动态内容时,需要执行这个sum逻辑 之后 元素已添加。因此,将它放在函数中,并在必要时调用它。

最简单的方法是创建一个 total 变量,该变量随 p 在一个 each() 循环,像这样:

$(document).ready(function() {
  // after you add the dymamic .slider p elements:
  calculateAndDisplayTotal();
});

function calculateAndDisplayTotal() {
  var total = 0;
  $('#cover div p').each(function() {
    total += parseInt($(this).text(), 10);
  });
  
  $('#resultTotal').text(total);
}
.slider {
  background-color: blue;
  color: white;
  padding: 5px 10px;
  margin: 10px;
}

#resultTotal {
  background-color: green;
  padding: 20px 10px;
  margin: 10px;
  color: white;
}
<div id="cover">
  <div class="slider">
    <p class="content">100</p>
  </div>
  <div class="slider">
    <p class="content">200</p>
  </div>
</div>

<div id="resultTotal"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
11 年前
回复了 Rory McCrossan 创建的主题 » 在jquery中获取多个复选框值时出现问题[duplicate]

你想要 :checkbox:checked 选择器和 map 要创建值数组,请执行以下操作:

var checkedValues = $('input:checkbox:checked').map(function() {
    return this.value;
}).get();

如果您的复选框中有一个共享类,则使用该类会更快。 $('.mycheckboxes:checked') ,或用于公共名称 $('input[name="Foo"]:checked')

6 年前
回复了 Rory McCrossan 创建的主题 » 如何使用jQuery更改TR的背景颜色?

你的逻辑有几个问题。首先你需要把所有的 tr 元素并单独处理它们。您还需要将选择器修复为 .myTabl 这个 table ,所以 桌子 选择器不正确。

如果你检查输出 css('background-color') 你会看到它在里面 rgb() 格式,不是十六进制或纯彩色名称。因此你需要在你的 if 条件。试试这个:

$(".myTabl tr").each(function() {
  if ($(this).css('background-color').toLowerCase() === "rgb(255, 255, 0)") {
    $(this).css("background-color", "#f00");
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table class="table table-bordered myTabl">
  <tr style="background:#ff0">
    <td>...</td>
  </tr>
  <tr style="background:#f00">
    <td>...</td>
  </tr>
  <tr style="background:#ff0">
    <td>...</td>
  </tr>
  <tr style="background:#f00">
    <td>...</td>
  </tr>
</table>

也就是说,如果你只是用类来设置颜色,那就简单多了。

6 年前
回复了 Rory McCrossan 创建的主题 » 使用js/jquery从附加的选择器选项中获取值

问题是你总是瞄准目标 $('.mySelector option:selected') . 这只会看到 .mySelector dom中的元素。

从逻辑的上下文来看,您似乎希望查看选定的选项 在内部 这个 select 使 change 事件。因此你需要使用 this find() . 还要注意,更新 .newDiv 循环完成后:

$(document).on('change', '.mySelector', function () {
  var str = "";
  $(this).find('option:selected').each(function () {
    str += $(this).text();
  });
  $('.newDiv').text(str);
});

通过使用 map() :

$(document).on('change', '.mySelector', function () {
  var str = $(this).find('option:selected').map(function () {
    return $(this).text();
  }).get().join('');
  $('.newDiv').text(str);
});
6 年前
回复了 Rory McCrossan 创建的主题 » 用jquery获取th的ID

如果你点击 <th> 然后您应该通过以下行获得ID:

var id=$(event.target).attr('id');

(如果单击包含事件的元素的元素,请使用“currentTarget”,而不是“target”)。

如果您单击其他东西,那么您需要通过parent()/child()或其他度量导航到那里。你有HTML吗?如果在使用$(event.target)时返回html,则返回哪个元素?

6 年前
回复了 Rory McCrossan 创建的主题 » 在jquery中通过构造函数设置类属性

问题是,您已经将参数名包装在 construct() 引号中的定义。这是语法错误。删除那些引号。

var ClassA = function(options) {
  var x;
  this.construct = function(value) { // change here
    this.x = value; // and here
  };
  this.construct("a");
}

$(document).ready(function() {
  var objA = new ClassA();
  console.log(objA.x);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

您应该注意,这个模式在很大程度上是多余的,但是只需使用 实际的 施工单位:

function ClassA(options) {
  this.x = options.value;
}

$(document).ready(function() {
  var objA = new ClassA({
    value: 'foo'
  });
  console.log(objA.x);
})
<script src=“https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js”></script>
5 年前
回复了 Rory McCrossan 创建的主题 » jquery click函数使用元素作为变量

问题是因为jquery对象没有 innerHTML 属性。

要实现所需的,请使用提供的元素对象 this 直接:

$('a.customclass').on("click", function() {
  var inner = this.innerHTML.replace(/ /g, "_");
  console.log(inner);
});

或者,使用jquery html() 方法:

$('a.customclass').on("click", function() {
  var inner = $(this).html.replace(/ /g, "_");
  console.log(inner);
});

我还建议您快速阅读 jQuery documentation ,因为即使快速浏览方法名称,您也可以很好地了解它们的用途和可能的用途。