2014-03-14 36 views
-1

我有一個包含多個文本字段的表單。如何禁用JavaScript中的表單中的一個字段?

在頂部的形式是兩個單選按鈕的一個問題:

去直接或上市。

  1. 轉到直接表示您必須提供一個電子郵件地址。

  2. 公開表示電子郵箱已禁用。

<input type="radio" name="target" value="public" /> 
<label for "public">Open to anyone </label></br> 
<input type="radio" name="target" value="direct"/> 
<label for="newEmail">Email address of the person:</label> 
<input type="text" name="newEmail" id='newEmail'> 
</br>  
</br> 
</br> 
<label for="title">Book title:</label> 
<input type="text" name="title" id='title'></br> 
<label for="location">Location:</label> 
<input type="text" name="location" id='location'> 

任何其他形式字段必須受到影響

+0

您使用的是ASP.net – Jegadeesh

+0

不是,這是一個PHP設置 – Maxcot

+0

當您選擇第二個選項時應該隱藏什麼? – sshow

回答

2

你可以做這樣的東西

$('input:radio').on('click',function(){ 
    if(this.checked && this.value == "public") // this.checked is not necessary as checking value already 
     $("#newEmail").prop("disabled",true); 
    else 
     $("#newEmail").prop("disabled",false); 
}); 

Fiddle

側面說明:我建議click代替change()因爲單選按鈕組中經常切換,您不需要添加多個案例或像複選框一樣使用條件邏輯。雖然也可以使用更改

+0

謝謝,非常感謝 – Maxcot

+0

@Maxcot歡迎朋友:) –

0

這將觸發基於該單選按鈕被選中上的電子郵件輸入的禁用狀態。

var $radios = $('input[type="radio"]'); 
$radios.change(function() { 
    $('#newEmail').prop('disabled', $radios.first().is(':checked')); 
}); 

JSFiddle

+0

這將是偉大的,如果你把它在改變事件 – Satpal

+0

@Satpal更喜歡它 – sshow

相關問題