2013-12-12 52 views
1

我有一组同名复选框。一个文本框与每个复选框关联。在页面加载时,我将禁用所有文本框。点击每个复选框后,应该启用对应于该复选框的文本框。复选框点击功能不起作用

问题是,我用不同的代码尝试启用复选框单击文本框。但复选框点击功能不起作用。

的代码如下(我使用MVC框架)给出:

{loopstart:setting:100} 
{if($setting%2==0)} 
<tr height="30"> 
{endif} 
<td align="left">   
<input type="checkbox" name="settingcheck" id="setting{$setting[0]}" value="{$setting[0]}"/>{$setting[1]} 
</td>             <td>             

<input type="text" size="2px" id="text{$setting[0]}">            


</td> 

               {if($setting%2==1)} 
               <td>&nbsp;</td> 
               {endif}  
            {loopend:setting} 

JQuery的

$(document).ready(function(){ 
    $("INPUT[type=text]").prop("disabled",true); 
    $("INPUT[type=text]").css("background-color", "#ccc"); 

}); 

这个代码是禁用页面加载的所有文本框和工作正常。

$('input[name='settingcheck']:checked').click(function() { 

} 

我写了上面的函数来启用对应的复选框单击文本框。但是这个功能不起作用。我试着在这个函数中提醒一些文本。

代码中是否有任何问题。任何人都可以帮助我解决这个问题。提前致谢。

+0

退房的语法高亮。 'setcheck'看起来像是可变的颜色还是字符串的颜色?;) –

回答

7

您的字符串字面量是不妥当的,因为你已经使用''附上文字字符串中的'所有出现的地方都被转义('input[name=\'settingcheck\']:checked'),也可以使用""代替

$(document).on('change', 'input[name="settingcheck"]', function() { 
    if(this.checked){ 
     //this is checked now 
    } else { 
     //this is unchecked now 
    } 
} 
+0

我已经试过这段代码很多次了。它不适合我。 – Jenz

+0

@Jenz看起来像你正在使用模板来生成元素...所以他们是动态的本质...你可能要尝试事件代表 –

+0

@Jenz看到更新 –

0

试试这个

$('input[name="settingcheck"]:checked').click(function() { 

} 
0

试试这个:

$("input[name='settingcheck']:checked").click(function() { 
} 
0

试试这个

$('input[name="settingcheck"]:checked').on('click',function() { 

}); 

OR

$(document).on('click', 'input[name="settingcheck"]:checked', function() { 

}); 

OR

$(document).on('change', 'input[name="settingcheck"]', function() { 

} 
1

如果尚未选中你可以试试这个

$('input[name="settingcheck"]').click(function() { 
    if($(this).is(':checked')){ 
     alert('checked'); 
    } 
}) 

看到demo

0

Working Demo

你可以尝试这样的:

<table> 
    <tr> 
     <td> 
      <input type="checkbox" onclick="checkBoxClicked1(this)" /> 
     </td> 
     <td> 
      <input type="text" style="display: none"> 
     </td> 
    </tr> 
    <tr> 
     <td> 
      <input type="checkbox" onclick="checkBoxClicked1(this)" /> 
     </td> 
     <td> 
      <input type="text" style="display: none"> 
     </td> 
    </tr> 
</table> 

的Javascript:

function checkBoxClicked1(checkbox) { 
     debugger; 
     var a = $(checkbox).is(':checked'); 
     var textBox = $(checkbox).closest("tr").children()[1].children[0]; 
     if (a) { 
      $(textBox).show(); 
      $(checkbox).prop("checked", true); 
     } else { 
      $(textBox).hide(); 
      $(checkbox).prop("checked", false); 
     } 

    }