jquery-selectors

jQuery check if any text input has value

假装没事ソ 提交于 2019-12-28 04:08:10
问题 I want to ask if there's a better way in jQuery to select multiple text input then check if any of them has a value. Here's my code: if ($("#reference").val() != "" || $("#pin").val() != "" || $("#fName").val() != "" || $("#mName").val() != "" || $("#datepicker").val() != "") { /*logic goes here */ } 回答1: You could do like below: if ($("#reference,#pin,#fName,#mName,#datepicker").filter(function() { return $(this).val(); }).length > 0) { //.. } Using a common function like the following would

Performance of jQuery selectors vs local variables

与世无争的帅哥 提交于 2019-12-28 04:01:33
问题 Is it recommended that, when I need to access the result of a jQuery selector more than once in the scope of a function, that I run the selector once and assign it to a local variable? Forgive my trite example here, but i think it illustrates the question. So, will this code perform faster: var execute = function(){ var element = $('.myElement'); element.css('color','green'); element.attr('title','My Element'); element.click(function(){ console.log('clicked'); }); } than this code: var

Using jQuery how do I select a range of rows?

随声附和 提交于 2019-12-28 03:12:09
问题 Does a jQuery only solution exist for selecting a range of rows from a table? I know 'eq', 'lt', 'gt' exist, but I'm looking for a combination of those selectors. 回答1: You can apply more than one filter at a time, although the 2nd filter applies to the results of the first, so the following would highlight starting from the 4th row (skips 0..2), and highlight for 3 rows (includes 0..2): $('#t tr:gt(2):lt(3)').css('background-color', '#f00'); 回答2: Because :gt() is a jQuery extension... using

jQuery selector for the label of a checkbox

五迷三道 提交于 2019-12-28 01:41:13
问题 <input type="checkbox" name="filter" id="comedyclubs"/> <label for="comedyclubs">Comedy Clubs</label> If I have a check box with a label describing it, how can I select the label using jQuery? Would it be easier to give the label tag an ID and select that using $(#labelId) ? 回答1: This should work: $("label[for='comedyclubs']") See also: Selectors/attributeEquals - jQuery JavaScript Library 回答2: $("label[for='"+$(this).attr("id")+"']"); This should allow you to select labels for all the fields

Assign click handlers in for loop

一世执手 提交于 2019-12-27 08:18:45
问题 I'm having several div's #mydiv1 , #mydiv2 , #mydiv3 , ... and want to assign click handlers to them: $(document).ready(function(){ for(var i = 0; i < 20; i++) { $('#question' + i).click( function(){ alert('you clicked ' + i); }); } }); But instead of showing 'you clicked 3' when click on #mydiv3 (as for every other click) I get 'you clicked 20' . What am I doing wrong? 回答1: It's a common mistake to create closures in loops in Javascript. You need to have some sort of callback function like

How to grab the value of the span element using jQuery

╄→гoц情女王★ 提交于 2019-12-26 11:39:32
问题 I have the following code: <tr> <td Width="50%" align="left"> <span id="ctl00_lblTotalDesc">Ext. Subtotal</span></td> <td Width="50%" align="right"> <span id="ctl00_lblTotalValue">100,087,000.00</span></td> </tr> I used the following to grab the value of the 2nd span element: spanValue = $('#ctl00_lblTotalValue').text(); But this doesn't seem to work in Spock/Geb. I get the following error: TypeError: $(...).text is not a function What am I doing wrong I get the following error if I use, $('

How to grab the value of the span element using jQuery

时光总嘲笑我的痴心妄想 提交于 2019-12-26 11:39:10
问题 I have the following code: <tr> <td Width="50%" align="left"> <span id="ctl00_lblTotalDesc">Ext. Subtotal</span></td> <td Width="50%" align="right"> <span id="ctl00_lblTotalValue">100,087,000.00</span></td> </tr> I used the following to grab the value of the 2nd span element: spanValue = $('#ctl00_lblTotalValue').text(); But this doesn't seem to work in Spock/Geb. I get the following error: TypeError: $(...).text is not a function What am I doing wrong I get the following error if I use, $('

Element IDs are not being recognised and are not triggering function

房东的猫 提交于 2019-12-26 08:25:50
问题 I'm trying to create simple selection option for users when selecting a particular font style. If they click on a font div , the background of that div should change color. The problem I'm having is that the first font div works perfectly in changing color - however the other font div s do not follow suit. JSFiddle HTML <div class="fonts"> <div id="font" class="minecraft-evenings"> Hello <p>Minecraft Evenings</p> </div> <div id="font" class="minecrafter"> Hello <p>Minecrafter 3</p> </div>

load a page with jQuery.ajax, how is it?

自作多情 提交于 2019-12-25 18:33:53
问题 why in this code after click not displaying(or not load) mysite.html? $('#icon a').click(function (event) { event.preventDefault(); $('.table_show, #num_count, #select_box, #input_search').fadeOut('slow', function () { $.ajax({ url: url, cache: false, success: function(html){ $(".results").append(html); $(this).hide().show(); //$.getScript("http://localhost/Siran-mehdi/files/js/admin.js"); } }); //.hide().show("slow") }); }); 回答1: $(".results").load('mysite.html'); Of course mysite.html must

load a page with jQuery.ajax, how is it?

為{幸葍}努か 提交于 2019-12-25 18:33:06
问题 why in this code after click not displaying(or not load) mysite.html? $('#icon a').click(function (event) { event.preventDefault(); $('.table_show, #num_count, #select_box, #input_search').fadeOut('slow', function () { $.ajax({ url: url, cache: false, success: function(html){ $(".results").append(html); $(this).hide().show(); //$.getScript("http://localhost/Siran-mehdi/files/js/admin.js"); } }); //.hide().show("slow") }); }); 回答1: $(".results").load('mysite.html'); Of course mysite.html must