表单内的输入按钮的数据值 [英] data value of input button inside a form

查看:139
本文介绍了表单内的输入按钮的数据值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < link rel ='stylesheet'href ='CSS / nook.css'type ='text / css'media ='screen,projection'/> 
< script type =text / javascriptsrc =JS / jquery-1.10.2.min.js>< / script>
< script>
$(document).ready(function()
{
$(。post_comment)。submit(function(e)
{
e.preventDefault );
var s = $(。comment)。data('i');
alert(s);
});
})
< /脚本>



for($ i = 0; $ i <2; $ i ++){
echo< form class ='post_comment' >中;
echo< div>< input type ='submit'class ='commment'data-i ='$ i'value ='$ i'/>< / div>
echo< / form>;
}
?>


解决方案

您需要找到输入元素提交表单

  $(document).ready(function(){
$(。post_comment)。submit (函数(e){
e.preventDefault();
var s = $(this).find(。comment)。data('i');
alert(s );
var value = $(this).find(。comment).val();
alert(value);
});
})

您的标记中还有一个拼写错误, class ='commment' code>应该是 class ='comment' - 在你的类名中有3 m



演示:小提琴



注意:使用控制台日志记录进行调试,而不是使用 alert()


I have a for loop that shows two forms with input button, how will i get the value of the input button?

   <link rel='stylesheet' href='CSS/nook.css' type='text/css' media='screen, projection' />
   <script type="text/javascript" src="JS/jquery-1.10.2.min.js"></script> 
   <script>
    $(document).ready(function() 
    {
    $(".post_comment").submit(function(e) 
      {
       e.preventDefault(); 
       var s = $(".comment").data('i');
       alert(s);
    });
   })
   </script>

   <?php

       for($i=0;$i<2;$i++){
         echo "<form class='post_comment'>";
         echo "<div><input type='submit'  class='commment'  data-i = '$i' value='$i'/></div>";
         echo "</form>";
        }
   ?>

解决方案

You need to find the input element which is inside the submitted form

$(document).ready(function () {
    $(".post_comment").submit(function (e) {
        e.preventDefault();
        var s = $(this).find(".comment").data('i');
        alert(s);
        var value = $(this).find(".comment").val();
        alert(value);
    });
})

You also have a spelling mistake in your markup, class='commment' should be class='comment' - there are 3 m in your class name

Demo: Fiddle

Note: Use console logging for debugging instead of alert()

这篇关于表单内的输入按钮的数据值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆