2013-01-22 7 views
-5

那朵我的代碼我想添加在HTML文本字段動態使用jQuery或JavaScript的一個特定的按鈕

<div class="rButtons"> 
    <input type="radio" name="numbers" value="10" />10 
    <input type="radio" name="numbers" value="20" />20 
    <input type="radio" name="numbers" value="other" />other 
</div> 

我想,當一個人選擇「其他」單選按鈕,他應該用文本字段中顯示他可以在那裏輸入價值。這個文本字段應該在其他的右邊。

另外我想的是,盒僅值的10

多個受限我爲新手,所以請更新代碼,並返回我回來。

使用jQuery,1.4.2.js

+2

返回asap?.... – mithunsatheesh

+0

你一定是在開玩笑我.. looks li你是否讓我們做你的工作?和儘快....夥計..FYI這個問題是最有可能很快關閉.. :) :) – bipen

+0

這聽起來不是一個問題,你不能指望我們代碼爲你 – phnkha

回答

0

我顯示你的JavaScript。 首先讓你的HTML文件看起來像這樣

<div class="rButtons"> 
    <input type="radio" name="numbers" value="10" onclick="uncheck();" />10 
    <input type="radio" name="numbers" value="20" onclick="uncheck();" />20 
    <input type="radio" name="numbers" value="other" onclick="check(this);"/>other 
    <input type="text" id="other_field" name="other_field" onblur="checktext(this);"/> 
</div> 

在第二個步驟寫這個CSS代碼初始設置文本字段不可見。

<style type="text/css"> 
#other_field 
{ 
    visibility: hidden; 
} 
</style> 

最後使用JavaScript代碼來驗證用戶的行爲

<script type="text/javascript"> 
    function uncheck() 
    { 
     document.getElementById('other_field').style.visibility = "hidden"; 
    } 
    function check(inputField) 
    { 
     if(inputField.checked) 
     { 
      document.getElementById('other_field').style.visibility = "visible"; 
     } 
    } 
    function checktext(inputField) 
    { 
     if(isNaN(inputField.value)) 
     { 
      alert('only numbers are allowed..'); 
      return false; 
     } 
     else if((inputField.value % 10) != 0) 
     { 
      alert('only multiples of 10..'); 
      return false; 
     } 
     else 
     { 
      return true; 
     } 

    } 
    </script> 

如果用戶點擊「其他」單選按鈕,顯示隱藏 文本提交的首份功能檢測..

第二個功能驗證輸入字段按照您的要求...

+0

花花公子你巖...我沒有檢查雖然.. :)非常感謝..我會回來與任何問題在我的腦海裏.. – Swati

+0

感謝很多老兄。它的工作原理... :) – Swati

+0

但一個小問題..在選擇另一個單選按鈕的文本框不會消散..它留在那裏:( – Swati

0

與你去做這種的jQuery JavaScript的。並嘗試先解決你的問題。

var item = $('input[name=numbers]:checked').val(); 
    if(item == 'other'){ 
    //Show your textbox 
    } 
+0

thans murtaza ..讓我試試然後我回到你身邊:) – Swati

+0

但我要在哪裏添加上面提到的這個文本?腳本標記內的 – Swati

+0

。 –

0

HTML:

<div class="rButtons"> 
    <input type="radio" name="numbers" value="10" />10 
    <input type="radio" name="numbers" value="20" />20 
    <input type="radio" name="numbers" value="other" /><span class="text-other">other</span> 
    <input class="user-input" style="display: none"></input> 
</div> 

的Jquery:

$('input[type=radio]').click(function() { 

    if ($(this).attr('value') == 'other') { 
     $('.text-other').hide(); 
     $('.user-input').show(); 
    } else { 
     $('.text-other').show(); 
     $('.user-input').hide(); 
    } 
}); 
相關問題