2014-09-21 11 views
-4

我試圖從jQuery if語句的span類中獲取TEXT。舉例來說,如果我有這樣的:jQuery爲jQuery'IF'語句獲取文本

if ($('input:radio[name=SELECT___ENG___57]:checked').val() == '1191') { 
$("[class^=eng_]").css("display", "none");} 

我想在input:radio[name=SELECT___ENG___57]:checked的「ENG」從<span class="product_code">ENG</span>(在同一頁面上的其他地方)動態值

所以基本上如果<span class="product_code">IMANOOB</span>我將如何獲得我的jQuery是

if ($('input:radio[name=SELECT___IMANOOB___57]:checked').val() == '1191') { 
$("[class^=eng_]").css("display", "none");} 

在此先感謝!

+0

您與選擇搞亂了那裏,只是使用像$僞選擇器( '跨度:包含( 「someValue中」)') – sarepta 2014-09-21 21:08:24

+0

你能發佈你的HTML的代表性樣本嗎?這可能比你的解釋更能說明你的問題(儘管如此,請嘗試並改進解釋)。如果可以,請在您的問題中加入現場演示(請參閱:http://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/?cb=1 )。 – 2014-09-21 21:19:00

回答

1

IM不知道如果我正確認識這個問題,但如果您試圖使選擇查詢的一部分依賴於跨度中的文本,則可以這樣做。

var productCode = $(".product_code").text(); 

if ($('input:radio[name=SELECT___'+productCode+'___57]:checked').val() == '1191') { 
$("[class^=eng_]").css("display", "none");} 

我不知道跨度產品代碼是如何設置的,並在那裏得到它的價值,但無論你設置跨度值,你應該刷新/重新調用選擇功能來更新頁面。

0

您只能檢查名字的beggining這樣的:

if ($('input:radio[name^=SELECT___]:checked').val() == '1191') { 
$("[class^=eng_]").css("display", "none");} 

因此,SELECT___ENG___57SELECT___IMANOOB___57SELECT___TEST將匹配並返回一個值

0

使用下面的代碼:

$(document).ready(function() { 
    var product_code = $('.product_code').text(); 
    if ($('input:radio[name=SELECT___'+ product_code +'___57]:checked').val() == '1191') { 
     alert('fired'); 
     $("[class^=eng_]").css("display", "none"); 
    } 
}); 

這裏是jsfiddle demo