簡體   English   中英

故障排除jquery啟用/禁用基於radiobutton的文本框

[英]Trouble shooting jquery enable/disable text box based on radiobutton

關於如何使用jquery基於單選按鈕選擇啟用文本字段有很多帖子。 來源: 使用單選按鈕(jQuery)禁用/啟用文本字段

我沒有使用javascript的經驗,我無法使用這些示例中的任何一個。

我嘗試過通過jfiddle提供的多個示例,但它們不適用於我的應用程序。

所以,一個清單:1)javascript一般工作,因為我復制並粘貼了一個日期選擇器小部件,工作得很好。 2)我的標簽包裹着代碼片段3)無論是在正文還是標題中都沒有區別。

這是我在視圖中使用的示例代碼(這是codeigniter)

<script src="//code.jquery.com/jquery-1.9.1.js">
//enable a box if the radio button is selected

$('input[name="radioknof"]').on('click', function() {
$(this).next().prop('disabled',false).siblings('input[type=text]').prop('disabled',true);
});
</script>

<input type="radio" name="radioknof" value="text1"/>
<input type="text" id="id1" disabled="disabled"/><br/><br/>
<input type="radio" name="radioknof" value="text2"/>  
<input type="text" id="id2" disabled="disabled"/>

顯然我錯過了一些像語法錯誤一樣簡單的東西......但我不能為我的生活看到它。 我不明白為什么jfiddle 100%工作,而在我的本地服務器上卻沒有。

(無論選擇單選按鈕如何,都會禁用這些框)

你有<script src="//code.jquery.com/jquery-1.9.1.js"> ,里面有代碼。 你應該有更多的東西:

  <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'></script>
  <script type='text/javascript'>
    // put your code in here
  </script>
</body>
</html>

或者在<head>使用load事件。 您無法定義src並將代碼放在<script>標記內。 我個人也會為你的其他標簽使用另一個外部src ,因此它會被緩存到你的客戶端的瀏覽器內存中。

工作了....正如我提到的我沒有經驗,所以我只是回到jfiddle示例並查看底層代碼並找到額外的行$(window).load(function(){ with closing }); 在末尾。

完整的代碼如下。

<html>
<body>
    <script type='text/javascript' src='//code.jquery.com/jquery-1.9.1.js'>  </script>
    <script type='text/javascript'>
        $(window).load(function(){
            $('input[type=text]').prop('disabled',true);
            $('input[name=radioknof]').on('click', function()
                {            
                    $(this).next().prop('disabled',true).siblings('input[type=text]').prop('disabled',false);
                }
            );
        });             
    </script>

    <input type="radio" name="radioknof" value="text1" />
    <input type="text" id="id1" disabled="true"/><br/><br/>
    <input type="radio" name="radioknof" value="text2"/>  
    <input type="text" id="id2"/> 
</body>

禁用的HTML表單輸入屬性不帶參數。 您要么設置了它,要么沒有設置,因此為了修復代碼,當您要啟用該字段時,完全刪除已禁用的屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM