dongshuo1856 2018-02-20 11:21
浏览 371
已采纳

获取url参数:如果在输入字段的name属性中使用了第三个括号,则JavaScript无效

I have used the below JavaScript code to get the parameter from the URL and checked the input field. It is working fine if I don't use name="community[]" third bracket in my input field. If I used third bracket in JavaScript code then it is not working.

html

<input type="checkbox" id="type" name="community[]" value="<?php echo $comvalue;?>">

JavaScript

var i = document.location.href.lastIndexOf('?');
document.location.href.substr(i+1).replace(/community=/g,'').split('&');
$('input[name="community"]').prop('checked',function(){
     return $.inArray(this.value,types) !== -1;
});
  • 写回答

1条回答 默认 最新

  • douliao8760 2018-02-20 11:24
    关注

    The issue is because you're missing the [] from the name attribute in your selector:

    var types = ['A', 'C'];
    
    $('input[name="community[]"]').prop('checked', function() {
      return $.inArray(this.value, types) !== -1;
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <input type="checkbox" id="type" name="community[]" value="A">
    <input type="checkbox" id="type" name="community[]" value="B">
    <input type="checkbox" id="type" name="community[]" value="C">

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题