Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 基于复选框值验证文本字段-jQuery验证_Php_Jquery - Fatal编程技术网

Php 基于复选框值验证文本字段-jQuery验证

Php 基于复选框值验证文本字段-jQuery验证,php,jquery,Php,Jquery,我有一个项目列表,每个项目都附有一个复选框。我目前正在使用jQuery表单和验证插件。我想使用它,这样只有当用户选中复选框时,才会验证与该复选框关联的文本字段,否则不需要验证 下面是演示的代码 <form action='feeddatabase.php' method=post id='feedlisting'> <input type=submit value='Submit!' name=submit> <table border=2> <

我有一个项目列表,每个项目都附有一个复选框。我目前正在使用jQuery表单和验证插件。我想使用它,这样只有当用户选中复选框时,才会验证与该复选框关联的文本字段,否则不需要验证

下面是演示的代码

<form action='feeddatabase.php' method=post id='feedlisting'>
<input type=submit value='Submit!' name=submit>

<table border=2>
    <input type=text name='title1' size=100 style='display:none' value="Babes with curves are the new rage in Bollywood">
 <tr>
<td rowspan="9"><input type='checkbox' class='article_check'  name='selectArticle[]'   value=1 /></td>
<td colspan="2"><h3><u><a href='http://www.santabanta.com/cinema.asp?pid=47880'>Babes with curves are the new rage in Bollywood</a></u></h3></td>
</tr>
<tr>
<td><b>Url</b></td>
 <td><input type=text  class='article_req_info' name='url1' size=100 value='http://www.santabanta.com/cinema.asp?pid=47880'>
                    <input type=text style='display:none' name='blogurl1' size=100 value='http://www.santabanta.com/cinema.asp'>
                    <input type=text style='display:none' name='blogtitle1' size=100 value="Latest Bollywood news, Movie Review and Previews">
                    <input type=text style='display:none' name='blogfeedurl1' size=100 value='http://www.santabanta.com/rss/cinema.asp?cat=Mirch Masala'></td>

 </tr>
 <tr>
 <td><b>Author</b></td>
 <td><input type=text  name='author1' size=100 value=''></td>
 </tr>

 <tr>
  <td><b>Date</b></td><td><input type=text name='date1' size=100 value='2011-7-08'> </td>
 </tr>

 <tr>
 <td><b>Desc</b></td>
  <td><input type=text name='desc1' size=100 value="The curve is more powerful than the sword. And the one time sex goddess should know this from her experience...">
                    <input type=text  style='display:none' name='html_content1'  value='The curve is more powerful than the sword. And the one time sex goddess should know this from her experience...'><br></td>
  </tr>

 <tr>
 <td><b>Featured</b></td><td><input type='checkbox' name='featuredArticle1' value=1 /></td>
 </tr>

 <tr>
 <td><b>Category</b></td>
 <td><select name="category1">
<option value="Technology"  >Technology</option>
<option value="Social" >Social</option>
<option value="Entertainment"selected >Entertainment</option>
<option value="Gaming"  >Gaming</option>
<option value="News"  >News</option>
<option value="Sports"  >Sports</option>
<option value="Videos"  >Videos</option></select></td>
 </tr>

 <tr>
 <td><u>Tags: </u></td>
 <td><input type=text name='tag1' size=100 value='new rage,curves,babes'></td>
 </tr>

 <tr>
 <td><u>Images for the post from Content/Google/Default</u></td>
 <td><input type=radio name="group1" checked value='http://media.santabanta.com/newsite/cinemascope/images/sonakshi24_big.jpg' \>
 </td>
 </tr>

 </table>
</form>

网址
作者
日期
描述

作为特色的 类别 技术 社会的 游戏娱乐 游戏 新闻 体育 视频 标签: 来自Content/Google/Default的帖子图片
am使用的jquery如下所示

<html> 
<head> 

 <script type="text/javascript" src="../javascripts/jquery.js"></script> 
<script type="text/javascript" src="../javascripts/jquery.form.js"></script> 
<script type="text/javascript" src="../javascripts/jquery.validate.js"></script> 
<script type="text/javascript" src="../javascripts/jquery.loady.js"></script> 


<script type="text/javascript">

 /* $("#loader").loady({
                    url: "sharer.php"
                }); */ 

$(document).ready(function() { 
    // bind form using ajaxForm 

        /* $(this).loady({
                    url: "sharer.php"
                }); */


        $('.article_check').click(function() {

             rules: {
                article_info: {
                    required: true,

                };  
            }
            });

});
</script>
 </head>
 <body>

</body>

</html>

/*$(“#装入器”).loady({
url:“sharer.php”
}); */ 
$(文档).ready(函数(){
//使用ajaxForm绑定表单
/*$(这个)({
url:“sharer.php”
}); */
$('.article_check')。单击(函数(){
规则:{
文章信息:{
要求:正确,
};  
}
});
});
如果我在某个地方错了,请纠正我

编辑:

现在我使用下面的脚本

 <script>

  $(document).ready(function(){
  $("#feedlisting").validate();
  }); 



 function countChecked() {
  var n = $("input[name='selectArticle[]']:checked").length;
  var v = $("input[name='selectArticle[]']:checked").val();

  if(v)
  {
  alert(v);
  alert("url"+v);
  $("#url"+v).addClass('required url');
  }
  else
  {

$("#url"+v).removeClass('required url');
  }
  $("div").text(n + (n <= 1 ? " is" : " are") + " checked!" + " value :" + v);
}
//countChecked();
$("input[name='selectArticle[]']:checkbox").click(countChecked);
</script>

$(文档).ready(函数(){
$(“#feedlisting”).validate();
}); 
函数countChecked(){
var n=$(“输入[name='selectArticle[]']:选中”).length;
var v=$(“输入[name='selectArticle[]']:选中”).val();
如果(v)
{
警戒(五);
警报(“url”+v);
$(“#url”+v).addClass('required url');
}
其他的
{
$(“#url”+v).removeClass('required url');
}
$(“div”).text(n+(n两个选项:
1.对于文本字段的有效性,您的验证功能将检查复选框的值,然后才验证文本字段。
2.将
onchange
事件添加到复选框中,这将从相关文本字段中添加/删除类
'required'
(请记住,也可以使用类属性在标记中定义验证规则)

编辑
针对您的问题,这里有一个简单的解决方案:

我假设您有一个要更改的文本字段的静态(=预先知道)列表。例如,如果这些字段是
txtName
txtdAddress
,则您的代码可能类似于:

$("#checkBox").change(function () { 
     if ($("#checkBox").val() {
          //if checkbox is checked- add the 'required' class
          $("#txtName").addClass('required');
          $("#txtAddress").addClass('required');
     }
     else {
          //if checkbox is unchecked- remove the 'required' class
          $("#txtName").rmeoveClass('required');
          $("#txtAddress").removeClass('required');
     }
});

好的……onchange事件目前看起来似乎是合理的,因为我能够获取所获取复选框的值。该值表示某个id。假设我选中的复选框的值为1,那么该复选框下的所有输入文本字段都有title1、headline1、dw1等名称……那么如何对onchange事件进行编码,以便添加“必需”att我很抱歉,我是javascript和jQuey的新手…不擅长用这种语言编码:(非常感谢Sjonny…如果我错了,请纠正我。我有一个已知文本字段和下拉列表、复选框和选择字段的列表。每个集合都有一个动态名称…我可以这样修改上面的代码吗$(“#txtName”+$(“#复选框”).val()).addClass('required')??可以,但我几乎不推荐。如果你知道控件的全名,为什么不使用它?如果所有字段都提前知道,为什么不提前命名?顺便说一句,我建议不要在变量/控件名称中使用数字。实际上,我知道最多要选择10个项目(复选框)每页。这些文章中的每一篇都有3个文本字段。例如:checkbox1将有txt_fld1_1 txt_fld2_1 txt_fld3_1,checkbox2将有txt_fld1_2,txt_fld2_2,txt_fld3_2等等。在运行时,用户可以选择任何复选框,根据复选框的值,我知道用哪个后缀来描述这些字段。只需检查我发布的代码d在我的原始问题中,您将了解我的方案。您对此方案有更好的解决方案吗?谢谢您的建议,伙计:)请注意我在原始问题中的编辑。它部分工作,但需要您的帮助