2014-11-05 73 views
0

大家好,我很新的jQuery和CSS和我試圖訪問以下內容:訪問元素的onclick

console

我試圖在onclick功能「檢查checkForAppointment'4個span標籤中的值。

HTML:

<div align="thun" id="clientScheduleWeek" data-role="page" data-theme="a"> 
    <div data-role="header" data-theme="a"> 
     <a href="#" onclick="JUMPclientDetails()">Back</a> 
     <h1>Client Details</h1> 
    </div> 
    <div data-role="content"> 
     <div id="weekViewBubble"> 
       <ul data-count-theme="b" data-inset="true" data-role="listview" data-theme="c"> 
        <li onClick="checkForAppointment();"><a>Monday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
        <li onClick="checkForAppointment();"><a>Tuesday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
        <li onClick="checkForAppointment();"><a>Wednesday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
        <li onClick="checkForAppointment();"><a>Thursday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
        <li onClick="checkForAppointment();"><a>Friday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
        <li onClick="checkForAppointment();"><a>Saturday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
        <li onClick="checkForAppointment();"><a>Sunday <span class="ui-li-count ui-btn-corner-all countBubl">0</span></a></li> 
       </ul> 
     </div> 
    </div> 
</div> 

我開始嘗試:

function checkForAppointment(){ 
    console.log('getting to the function anyway'); 
    var j = $('ul li').eq(0).value; 
    var k = $('ul li').eq(1).value; 

    console.log('j is ' + j + ' and k is '+ k); 
}; 

兩個J和K在這一點上是不確定的

編輯:

功能由lolka_bolka建議
function checkForAppointment(){ 
    var AptBool=false; 
    $('.something').click(function() { 
     console.log('we have entered the .click function'); 
     //Get all the span inside of it 
     var collection = $(this).find('span'); 

     //Loop through all span object 
     $.each(collection, function(idx, obj) { 
      if ($(obj).html() > 0){ 
       AptBool=true; 
       //return; 
      } 

      //Write the html value of span to the console. 
      //console.log('value here is ' + $(obj).html()); 
     }); 

     if (!AptBool){ 
      alert('There is no appointment to view'); 
      return; 
     } 

    }); 


    //console.log('getting to the function anyway'); 
    //var j = $('ul li').eq(0).text(); 
    //var k = $('ul li').eq(1).text(); 

    //console.log('j is ' + j + ' and k is '+ k); 
}; 
+0

難道這應該是XML或HTML? ' http://54.72.173.92/console.png' – Brewal 2014-11-05 15:02:57

+1

向我們展示您的html請在您的問題中,而不是在圖片上。 – vaso123 2014-11-05 15:03:02

+1

jQuery中的所有方法都會返回一個jQuery對象,這是一個DOM集合的包裝器對象。因此,當您調用.eq(0)時,將返回表示集合元素集的jQuery對象。要獲得該值,您可以調用.eq(0).val() – edrian 2014-11-05 15:04:33

回答

1

因爲你不爲我們提供的HTML代碼,你證明了一個圖像,而不是,我只是做一個小例子,我希望你能理解它的邏輯:

HTML

<li class="something"> 
    Click here 
    <div> 
     <span>0</span> 
     <span>1</span> 
     <span>2</span> 
     <span>3</span> 
    </div> 
</li> 

jQuery的

$(function() { 
    //On li click where li has .something class 
    $('.something').click(function() { 

     //Get all the span inside of it 
     var collection = $(this).find('span'); 

     //Loop through all span object 
     $.each(collection, function(idx, obj) { 
      //Write the html value of span to the console. 
      console.log($(obj).html()); 
     }); 
    }); 
}); 
+0

感謝此工作對我 – user2363025 2014-11-05 15:22:47

+0

不客氣。 – vaso123 2014-11-05 15:24:26

+0

我已經添加了一點到你的功能,它似乎當我點擊一個列表項,我多次進入該功能。我不懂爲什麼?我會編輯我的原始問題,向您展示代碼 – user2363025 2014-11-05 15:40:49