【问题标题】:Laravel : How to insert checkbox values with submit button? [closed]Laravel:如何使用提交按钮插入复选框值? [关闭]
【发布时间】:2017-03-29 09:02:06
【问题描述】:
<form action="button" method="POST">`
<input type="checkbox" name="feature1"  id="clickme" ><b>Select<b/>`
</form> 

<script src="<?php echo url(); ?>/assets/js/jquery.min.js"></script>    
<script>
$(document).ready(function() {
$('#clickme').click(function()
{ 
    if($(this).is(':checked'))
    {
        $(this).val('1');
        alert('value is set 1');
    }
    else
    {
        $(this).val('0');
        alert('value is set 0');
    }
});    

});

【问题讨论】:

  • 请努力使代码格式正确。
  • 请不要将代码发布到 cmets。编辑您的原始帖子以添加任何新信息。

标签: php jquery laravel


【解决方案1】:

$(document).ready(function() {
$('#clickme').click(function()
{ 
    if($(this).is(':checked'))
    {
var checkedVal='';
//insert.php write insert code in PHP
     checkedVal=$(this).val('1');
	 $.ajax({url: "insert.php", 
	 data:{cheecked_val:checkedVal},
	 success: function(result){
        alert('Data Transferred successfully');
    }});
    }
    else
    {
        $(this).val('0');
        alert('value is set 0');
    }
});    

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<input type="checkbox" id="clickme" />  Check Hear

【讨论】:

  • hai 我给了赋值,但只选择了一个复选框,另一个框没有选择我能做什么.......
  • 如果你想选中所有复选框,你可以为所有复选框分配类并使用 $.each 方法。你会得到完美的解决方案。
【解决方案2】:

您不需要这样做。只需添加您的复选框并设置值。

<input type="checkbox" name="feature1" value="1"><b>Select<b/>

这个工作原理是这个复选框的值只有在它被选中时才会被传递,所以不需要在 1 和 0 之间切换值。你需要做的就是检查输入是否存在。

if (request()->has('feature1')) {
    // Code if feature1 has been checked
}

【讨论】:

    猜你喜欢
    • 2023-02-15
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多