【问题标题】:jquery trigger is not passing radio button valuejquery 触发器未传递单选按钮值
【发布时间】:2019-08-10 19:15:51
【问题描述】:

我有一个单选按钮(是,否)和输入文本框。 如果用户按否,输入字段将被禁用,并且 jquery .triger('change') 用于 ajax 表单提交(其中我已启动 ajax

$('#form-id').change(function(){
....
});

具有上述功能)

但是,当检查是的,输入就会变得活跃,用户可以输入值并按照往常按下提交按钮。上面的 ajax 更改功能很好地检测到用户将键入并失去焦点时的更改。所以这部分没有问题。

我的问题是当用户勾选 no 时,php 表单提交中的 isset($_post['radio_no']) 参数显示为空白。相反,我需要显示 value='No'。

我所有的努力都在进行。谁能帮帮我。

我的 Jquery

$(document).ready(function () {

$('input').on('ifClicked', function (event) {
    var value = $(this).val();
    if(value=='No'){
    $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").prop('disabled', true);
    $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").val('0').trigger('change');
    } else {
    $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").val(''); 
    $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").prop('disabled', false);
    }
        });
        });

【问题讨论】:

    标签: jquery html ajax triggers


    【解决方案1】:

    似乎调整 PHP 方面可能是最简单的方法......

    if (empty($_POST['radio_no'])) $_POST['radio_no'] = 'No';
    

    如果 PHP 方法不是一个选项,请回复,我将为您提供前端解决方案。这只是更多的工作。

    【讨论】:

    • 嗨@James Allen,这没有帮助。实际上还有另一个实例检查空位。所以你有什么其他的建议来解决它。谢谢
    【解决方案2】:

    最后我自己通过修改上面的Jquery找到了答案。我的最终 Jquery 如下。

    $(document).ready(function () {
    
    $('input').on('ifClicked', function (event) {
        var value = $(this).val();
        if(value=='No'){
        $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").prop('disabled', true);
        $('input:radio[name=radio][value=Yes]:checked').prop('checked', false).iCheck('update'); // Added
        $('input:radio[name=radio]').filter('[value=No]').prop('checked', true); // Added
        $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").val('0').change();
        } else {
        $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").val(''); 
        $(this).closest('body').find("input[data-value=" + this.name + "][type=text]").prop('disabled', false);
        }
            });
            });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-14
      • 2013-08-26
      • 1970-01-01
      • 2015-05-26
      • 2017-02-03
      相关资源
      最近更新 更多