国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
Jquery和javascript常用技巧

        var objSel = document.getElementById("selOp"); 
         
        //這是獲取值 
        alert("當(dāng)前值: " + objSel.value); 
         
        //這是獲取文本 
        alert("當(dāng)前文本: " + objSel.options(objSel.selectedIndex).text); 

 

 

增加復(fù)選框套路
var e = document.createElement("input");
  e.type = "checkbox";
  e.value = result.content[i].id;
  fbox.appendChild(e);
  fbox.appendChild(document.createTextNode(result.content[i].name+"    "));


刪除子節(jié)點(diǎn)
var fbox = document.getElementById("chbox");
fbox.innerHTML ='';  

<div id="chbox"></div>

 

動(dòng)態(tài)添加下拉框節(jié)點(diǎn)

var eles = document.forms['theForm'].elements;
   eles['goodsList'].length = 1;
   for (i = 0; i < result.content.length; i++)
   {
     var opt = document.createElement('OPTION');
     opt.value = result.content[i].id;
     opt.text  = result.content[i].name;
     eles['goodsList'].options.add(opt);
   }

 

 

中文檢測(cè)的正則

function   checkStr(str)   //中文值檢測(cè)

var reg =/^[\u4e00-\u9fff]*$/;
      if(!reg.test(str)){
       return true;
      }
      return false;
}


注意網(wǎng)上的 [\u4e00-\u9fa5]區(qū)間不對(duì)

 

 var reg= /^[1][3458]\d{9}$/; //驗(yàn)證手機(jī)號(hào)碼 
      if(!reg.test(str)){
       return false;
      }
      return true;

 

//jquery bug IE7,8不能取到單選按鈕的選中狀態(tài),IE9 可以
//if($("#rd_getsupplierinfo:checked").get(0).checked == true)

這個(gè)辦法可以
if($("#rd_getsupplierinfo").attr("checked")=="checked")

 

//jquery bug IE7,8不支持

$("span[class*='allprice']").each(function() {

只能用

 $("span").each(function(){   if($(this).attr("class")=="allprice"){

遍歷

$("button").click(function(){  $("li").each(function(){    alert($(this).text())  });});

 

文本框立輸入值立即觸發(fā)事件

<input type="number" min="0" step="1" name="goods_num" id="goods_number" size="10" value=""  oninput="alert('7')" onpropertychange="alert(1)" required="required"/>

 

動(dòng)態(tài)id
var id = 'one';
var el = $('#' + id);

 

遍歷

$("span[class*='price']").each(function()

 {  

 $('#count').text(parseInt($('#count').text())+parseInt($(this).text()));            

 });

 

$("select").each(
  function()
  {
    if($(this).attr('class')=="brand")
          {
             if($(this).val()=="0"||$(this).val()==null)
                 {
                     validator.addErrorMsg("不能為空");
                 }
              
          }
 
  });

遍歷下拉框

 

 

jquery驗(yàn)證

$("parent child"),如$("div>span")選取<div>元素下元素名是<span>的子元素
$('prev+next')等價(jià)$('prev').next('next'),如$('.one+div')等價(jià)$('.one').next('div')是選取class為one的下一個(gè)<div>標(biāo)簽元素
$('prev~siblings')等價(jià)$('prev').nextAll('div'),如('#two~div')等價(jià)$('#two').nextAll('div')是選取id為two的元素后面的所有<div>兄弟元素
3、過濾選擇器:主要是通過一些過濾規(guī)則來(lái)篩選DOM元素,過濾規(guī)則與CSS中的偽類選擇器語(yǔ)法相同,即選擇器都以一個(gè)冒號(hào)(:)開頭
$("標(biāo)簽元素:first"),如$("div:first")是選取所有<div>元素中第一個(gè)<div>元素

 

js父節(jié)點(diǎn)

this.parentNode.id
this.parentNode.parentNode.id

 

增加節(jié)點(diǎn)

$("p").before( $("b");在每個(gè)匹配的元素之前插入內(nèi)容。
表示p的前面是b,也就是b要插到p的前面。

$("p").insertBefore("b");表示將p插入到b的前面

$("p").insertAfter("#foo");把所有匹配的元素插入到另一個(gè)、指定的元素元素集合的后面。

$("p").append("<b>Hello</b>");向每個(gè)匹配的元素內(nèi)部追加內(nèi)容,這個(gè)操作與對(duì)指定的元素執(zhí)行

appendChild方法,將它們添加到文檔中的情況類似。

$("p").remove();
從DOM中刪除所有匹配的元素。

 

 

span

$("#fok").text("aaaaaaaa");

$("#fok").html("aaaaaaaa");

$(document).ready(function(){    
    $("#resetbtn").click(function(){ 
        $("#user_id").val("");//清空 
        $("#realname").val("www");//賦值 
   }); 
});

 

設(shè)置select的value值為4的項(xiàng)選中: $("#slc1 ").val(4); 

 

頁(yè)面有多個(gè)下拉框,需要遍歷判斷

非主動(dòng)觸發(fā),例如統(tǒng)一驗(yàn)證

$("select").each(
  function()
  {alert($(this).attr('name'));
   alert($(this).val())
alert($(this).text());當(dāng)前所有text
alert($(this).find("option:selected").text());當(dāng)前選中text
  });

 

 

主動(dòng)觸發(fā)

<select name="s_id[{$goods.goods_id}]" onchange="changBrandEach(this)">
function changBrandEach(sld)
                {
                    alert($(sld).val());

js獲取id和name
sld.id   sld.name

單個(gè)
var s_id = $("#s_id ").val();

說明如果是動(dòng)態(tài)的就去掉#和""

 

jquery獲得下拉框的值
獲取Select :
 獲取select 選中的 text :
   $("#ddlRegType").find("option:selected").text();
 
 獲取select選中的 value:
   $("#ddlRegType ").val();
 
 獲取select選中的索引:
     $("#ddlRegType ").get(0).selectedIndex;
 
設(shè)置select:
 設(shè)置select 選中的索引:
     $("#ddlRegType ").get(0).selectedIndex=index;//index為索引值
 
 設(shè)置select 選中的value:
    $("#ddlRegType ").attr("value","Normal“);
    $("#ddlRegType ").val("Normal");
    $("#ddlRegType ").get(0).value = value;
 
 設(shè)置select 選中的text:
var count=$("#ddlRegType option").length;
  for(var i=0;i<count;i++) 
     {           if($("#ddlRegType ").get(0).options[i].text == text) 
        { 
            $("#ddlRegType ").get(0).options[i].selected = true; 
         
            break; 
        } 
    }
 
$("#select_id option[text='jQuery']").attr("selected", true);
 
設(shè)置select option項(xiàng):
 
 $("#select_id").append("<option value='Value'>Text</option>");  //添加一項(xiàng)option
 $("#select_id").prepend("<option value='0'>請(qǐng)選擇</option>"); //在前面插入一項(xiàng)option
 $("#select_id option:last").remove(); //刪除索引值最大的Option
 $("#select_id option[index='0']").remove();//刪除索引值為0的Option
 $("#select_id option[value='3']").remove(); //刪除值為3的Option
 $("#select_id option[text='4']").remove(); //刪除TEXT值為4的Option

 

 

動(dòng)態(tài)增加下拉選項(xiàng)


var eles = document.forms['theForm'].elements;
                    eles[brand_id].length = 0;//這句很重要,否則會(huì)以疊加的形式出現(xiàn)
                    for (i = 0; i < result.content.length; i++)
                    {
                    var opt = document.createElement('OPTION');
                    opt.value = result.content[i].brand_id;
                    opt.text  = result.content[i].brand_name;
                    eles[brand_id].options.add(opt);
                    } 
清空 Select:
$("#ddlRegType ").empty();

 

1,下拉框:
var cc1  = $(".formc select[@name='country']  option[@selected]").text(); //得到下拉菜單的選中項(xiàng)的文本(注意中間有空格)
var cc2 = $('.formc  select[@name="country"]').val();  //得到下拉菜單的選中項(xiàng)的值
var cc3 = $('.formc  select[@name="country"]').attr("id");  //得到下拉菜單的選中項(xiàng)的ID屬性值
$("#select").empty();//清空下拉框//$("#select").html('');
$("<option  value='1'>1111</option>").appendTo("#select")//添加下拉框的option
稍微解釋一下:
1.select[@name='country']  option[@selected] 表示具有name 屬性,
并且該屬性值為'country' 的select元素 里面的具有selected  屬性的option  元素;
可以看出有@開頭的就表示后面跟的是屬性。

2,單選框:
$("input[@type=radio][@checked]").val();  //得到單選框的選中項(xiàng)的值(注意中間沒有空格)
$("input[@type=radio][@value=2]").attr("checked",'checked');  //設(shè)置單選框value=2的為選中狀態(tài).(注意中間沒有空格)

3,復(fù)選框:
$("input[@type=checkbox][@checked]").val();  //得到復(fù)選框的選中的第一項(xiàng)的值
$("input[@type=checkbox][@checked]").each(function(){  //由于復(fù)選框一般選中的是多個(gè),所以可以循環(huán)輸出
  alert($(this).val());
  });

$("#chk1").attr("checked",'');//不打勾
$("#chk2").attr("checked",true);//打勾
if($("#chk1").attr('checked')==undefined){}  //判斷是否已經(jīng)打勾

javascript 獲得下拉框選中值
var index = document.getElementById("DropDownList1").selectedIndex;
var v=document.getElementById("DropDownList1").options[index].value;

===============

下拉框值選中

/獲取第一個(gè)option的值 
$('#test option:first').val(); 
//最后一個(gè)option的值 
$('#test option:last').val(); 
//獲取第二個(gè)option的值 
$('#test option:eq(1)').val(); 
//獲取選中的值 
$('#test').val(); 
$('#test option:selected').val(); 
//設(shè)置值為2的option為選中狀態(tài) 
$('#test').attr('value','2'); 
//設(shè)置第一個(gè)option為選中 
$('#test option:last').attr('selected','selected'); 
$("#test").attr('value' , $('#test option:last').val()); 
$("#test").attr('value' , $('#test option').eq($('#test option').length - 1).val()); 
//獲取select的長(zhǎng)度 
$('#test option').length; 
//添加一個(gè)option 
$("#test").append("<option value='9'>ff</option>"); 
$("<option value='9'>ff</option>").appendTo("#test"); 
//添除選中項(xiàng) 
$('#test option:selected').remove(); 
//指定項(xiàng)選中 
$('#test option:first').remove(); 
//指定值被刪除 
$('#test option').each(function(){ 
if( $(this).val() == '5'){ 
$(this).remove(); 

}); 
$('#test option[value=5]').remove(); 

//獲取第一個(gè)Group的標(biāo)簽 
$('#test optgroup:eq(0)').attr('label'); 
//獲取第二group下面第一個(gè)option的值 
$('#test optgroup:eq(1) :option:eq(0)').val(); 

獲取select中選擇的text與value相關(guān)的值 

獲取select選擇的Text : var checkText=$("#slc1").find("option:selected").text(); 
獲取select選擇的value:var checkValue=$("#slc1").val(); 
獲取select選擇的索引值: var checkIndex=$("#slc1 ").get(0).selectedIndex; 
獲取select最大的索引值: var maxIndex=$("#slc1 option:last").attr("index"); 

設(shè)置select選擇的Text和Value 

設(shè)置select索引值為1的項(xiàng)選中:$("#slc1 ").get(0).selectedIndex=1; 
設(shè)置select的value值為4的項(xiàng)選中: $("#slc1 ").val(4); 
設(shè)置select的Text值為JQuery的選中: 
$("#slc1 option[text='jQuery']").attr("selected", true); 
PS:特別要注意一下第三項(xiàng)的使用哦??纯碕Query的選擇器功能是如此地強(qiáng)大呀! 

添加刪除option項(xiàng) 

為select追加一個(gè)Option(下拉項(xiàng)) 
$("#slc2").append("<option value='"+i+"'>"+i+"</option>"); 
為select插入一個(gè)option(第一個(gè)位置) 
$("#slc2").prepend("<option value='0'>請(qǐng)選擇</option>"); 
PS: prepend 這是向所有匹配元素內(nèi)部的開始處插入內(nèi)容的最佳方式。 
刪除select中索引值最大option(最后一個(gè)) 
$("#slc2 option:last").remove(); 
刪除select中索引值為0的option(第一個(gè)) 
$("#slc2 option[index='0']").remove(); 
刪除select中value='3'的option 
$("#slc2 option[value='3']").remove(); 
刪除select中text='4'的option 
$("#slc2 option[text='3']").remove();

=====================

驗(yàn)證

validator = new Validator("theForm");   

validator.required("goods_number","商品數(shù)量不能為空");   

validator.isNumber("goods_number","商品數(shù)量必須為數(shù)字");   

if($("#rd_tuan_price").attr("checked")==undefined)    {       

validator.required("tuan_price_my","自定義團(tuán)購(gòu)價(jià)格不能為空");       

validator.isNumber("tuan_price_my","自定義團(tuán)購(gòu)價(jià)格必須為數(shù)字");    }       

 if(validator.passed()==false)   

 {           

 return false;   

 }

打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
jquery獲得option的值和對(duì)option進(jìn)行操作
jQuery-對(duì)Select的操作集合[終結(jié)篇] - 博客文庫(kù) - 博客園
淺談jquery關(guān)于select框的取值和賦值
jQuery-對(duì)Select的操作集合
JQUERY獲取text,areatext,radio,checkbox,select值
jQuery對(duì)select操作小結(jié) - 鬼鬼DH - 博客園
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服