jQuery添加options點(diǎn)擊事件并傳值實(shí)例代碼
說明:
最近剛接了一個(gè)項(xiàng)目,其中有項(xiàng)目需求要求根據(jù)選擇不同店鋪選項(xiàng),上送不同id值,展示不同商品列表
先給大家展示下實(shí)現(xiàn)效果圖:如果大家感興趣對(duì)此很感興趣,可以參考下實(shí)現(xiàn)代碼。

var formStr = "{'supplierId':'供應(yīng)鏈企業(yè)|%-jm-sprt-%|93794498-3'}";
$.ajax({
type : "post",
dataType : "json",
cache : false,
url : "../../../webservice/gmall.json", // 提交到一般處理程序請(qǐng)求數(shù)據(jù)
data :"businessCode=61001001&message="+encodeURI(formStr).replace(/\+/g, "%2B"),
async : false,
error : function() {
//alert("connection error!!!");
layer.alert("數(shù)據(jù)加載失敗,請(qǐng)及時(shí)聯(lián)系管理員!",0);
},
success : function(data) {
var dataJson = strToJson(data.msg);
var shopname = dataJson.body.gmPage;
if(dataJson.head.status=='000000'){
$.each(shopname,function(i,item){
$("#store").append("<option id='"+item.id+"' value='"+item.shopType+"' title='"+item.name+"'>"+item.name+"</option>");
})
$("select").on('click',function (){
var opt = $(this).children('option:selected');
var shopId = $(opt).attr('id');
var shopType = $(opt).val();
var formStrshop = " {'shopId':"+shopId+",'shopType':"+shopType+"}";
if($("option").val()==''){
$("#tb").empty();
}
$.ajax({
type : "post",
dataType : "json",
cache : false,
url : "../../../webservice/gmall.json", // 提交到一般處理程序請(qǐng)求數(shù)據(jù)
data :"businessCode=61001003&message="+encodeURI(formStrshop).replace(/\+/g, "%2B"),
async : false,
error : function() {
layer.alert("數(shù)據(jù)加載失敗,請(qǐng)及時(shí)聯(lián)系管理員!",0);
},
success : function(data) {
var dataJson = strToJson(data.msg);
var shopname = dataJson.body.gmPage;
if(dataJson.head.status=='000000'){
$.each(shopname,function(i,item){
var attributeValue11 = dataJson.body.gmPage[i].attributeValue11;
$("#tb").empty();
$("#tb").append("<tr id='"+item.id+"'>"
+"<td><input type='checkbox' value='"+item.id+"' name='price'/></td>"
+"<td>"+item.sn+"</td>"
+"<td>"+item.name+"</td>"
+"<td>"+item.attributeValue11+"</td>"
+"<td><input type='text' name='discount' class='text' style='width:100px'/></td>"
+"<td>"+0+"</td></tr>"
);
})
}
}
});
})
}
}
});
簡單示例
<select name="" id="">
<option value="1" id="1">123</option>
<option value="2" id="2">gfg</option>
<option value="3" id="3">ds3</option>
<option value="4" id="4">1fgtr3</option>
<option value="5" id="5">6y3</option>
<option value="6" id="6">1fbnh3</option>
<option value="7" id="7">1vdf3</option>
</select>
<script src="jquery-1.8.2.min.js"></script>
<script>
$(function(){
$('select').on('click',function(){
// $(this).on('click',function(){ 此步可省略,無需管option的點(diǎn)擊事件
var $opt = $(this).children('option:selected');
console.log($($opt).attr('id'),$($opt).val(),$($opt).html());
// })
})
})
</script>
以上所述是小編給大家介紹的jQuery添加options點(diǎn)擊事件并傳值實(shí)例代碼的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- jquery無法為動(dòng)態(tài)生成的元素添加點(diǎn)擊事件的解決方法(推薦)
- jquery 觸發(fā)a鏈接點(diǎn)擊事件解決方案
- jQuery實(shí)現(xiàn)當(dāng)按下回車鍵時(shí)綁定點(diǎn)擊事件
- jQuery實(shí)現(xiàn)按鈕只點(diǎn)擊一次后就取消點(diǎn)擊事件綁定的方法
- jquery中交替點(diǎn)擊事件的實(shí)現(xiàn)代碼
- jquery實(shí)現(xiàn)點(diǎn)擊label的同時(shí)觸發(fā)文本框點(diǎn)擊事件的方法
- jQuery中slideUp 和 slideDown 的點(diǎn)擊事件
- jQuery實(shí)現(xiàn)移動(dòng) 和 漸變特效的點(diǎn)擊事件
- jQuery實(shí)現(xiàn)點(diǎn)擊行選中或取消CheckBox的方法
- jQuery移除或禁用html元素點(diǎn)擊事件常用方法小結(jié)
相關(guān)文章
jquery 獲取json數(shù)據(jù)實(shí)現(xiàn)代碼
jquery json數(shù)據(jù)獲取代碼。2009-04-04
基于jquery實(shí)現(xiàn)的定時(shí)顯示與隱藏div廣告的實(shí)現(xiàn)代碼
有時(shí)候我們需要在jquery中要顯示與隱藏層,很簡單的方法就是直接使用hide與show方法就可以了,但要定時(shí)我們需要結(jié)合setTimeout函數(shù)來實(shí)現(xiàn)2013-08-08
jQuery實(shí)現(xiàn)鼠標(biāo)移到某個(gè)對(duì)象時(shí)彈出顯示層功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)鼠標(biāo)移到某個(gè)對(duì)象時(shí)彈出顯示層功能,涉及jQuery基于事件響應(yīng)動(dòng)態(tài)操作頁面元素屬性相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-08-08
jQuery+HTML5實(shí)現(xiàn)圖片上傳前預(yù)覽效果
這篇文章主要介紹了jQuery+HTML5實(shí)現(xiàn)圖片上傳前預(yù)覽效果,涉及jquery操作圖片動(dòng)態(tài)顯示效果,非常簡單實(shí)用,需要的朋友可以參考下2015-08-08
讓頁面上兩個(gè)div中的滾動(dòng)條(滑塊)同步運(yùn)動(dòng)示例
如何想讓頁面上兩個(gè)div中的滾動(dòng)條(滑塊)同步運(yùn)動(dòng)該怎實(shí)現(xiàn),具體代碼如下,感興趣的朋友可以參考下2013-08-08
JQuery中attr方法和removeAttr方法用法實(shí)例
這篇文章主要介紹了JQuery中attr方法和removeAttr方法用法,實(shí)例分析了jQuery中attr方法設(shè)置屬性與removeAttr方法移除屬性的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05

