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)證
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();
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;
}
聯(lián)系客服