2012-09-24 69 views
1

我需要一個JavaScript,可以將屬性插入字段,並在用戶取消選擇該字段時將其刪除。當用戶選擇收音機時,將屬性插入輸入字段

下面是我使用的無線電代碼選擇:

<script> 
$(document).ready(function() { 
    $("input[name$='sel']").click(function() { 
     var test = $(this).val(); 

     $("div.desc").hide(); 
     $("#sel" + test).show(); 
    }); 

    if (test == "CreditCard") { 
     $("#card").addClass("validate[custom[card]] text-input"); 
    } else { 
     $("#card).removeClass("validate[custom[card]] text-input"); 
    } 
}); 
</script> 

字段代碼:

<table><tr><td> 

<p> 
Credit/Debit Card<lable style="margin-right:1px;"></lable><input type="radio" name="sel" id="sel" value="CreditCard" /> 
</p> 
<p> 
Paypal<input type="radio" id="sel" name="sel" value="Paypal" /> 
</p> 
<p> 
Wire Transfer<input type="radio" id="sel" name="sel" value="WireTransfer" /> 
</p> 
<!====================================================================================> 
</td><td> 
<!====================================================================================> 
<div id="selCreditCard" class="desc"> 

<label for="card"><strong>Card Number<font color=red size=3> *</font></strong></label> 
     <input name="card" type="text" id="card" value="" style="width:85px;" /> 
</div> 
<!====================================================================================> 
<div id="selPaypal" class="desc" style="display: none;margin-left:20px;margin-top:1px;margin-bottom:1px;"> 
    paypal 
</div> 
<!====================================================================================> 
<div id="selWireTransfer" class="desc" style="display: none;margin-left:20px;margin-top:0px;margin-bottom:-5px;"> 

Transfer 

</div> 
<!====================================================================================> 
</td></tr></table> 
​ 

這是一個需要被插入到「卡」輸入字段屬性:

class="validate[custom[card]] text-input" 

當用戶選擇與該值CreditCard收音機,該屬性將被插入到輸入字段,並且當用戶取消選擇值爲CreditCard的廣播時,該屬性將被刪除。

鏈接,小提琴: FIDDLE

+0

你似乎是使用jQuery。你可以使用'.addClass'和'.removeClass'。 – bfavaretto

回答

0

telexper,有很多的代碼中的錯誤的東西,可惜的是我沒有時間,現在來解釋。

,但我會離開這裏的工作代碼爲你:http://jsfiddle.net/RASG/DSrLS/

只需點擊「信用卡/借記卡」和類nowitsadded西港島線被添加到輸入字段,它會變成紅色。

HTML

<input type="radio" id="sel1" name="sel"> Credit/Debit Card 

<br> 

<input type="radio" id="sel2" name="sel"> Paypal 

<br> 

<input type="radio" id="sel3" name="sel"> Wire Transfer 

<br> 
<br> 

<div id="selCreditCard" class="desc"> 
    Card Number <input name="card" type="text" id="card" value=""> 
</div> 

JS

$('#sel1').click(function() { $('#card').addClass('nowitsadded') }) 

CSS

.nowitsadded {background-color: red} 
+0

如何添加此類=「驗證[自定義[卡]]文本輸入」到輸入字段 – telexper

+0

好吧,那麼如何添加此屬性? – telexper

+0

我剛剛向你展示瞭如何添加一個類!用你的班級名稱替換'nowitsadded'。 – RASG

相關問題