Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.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
jQuery submit();没有增值_Jquery_Forms_Button_Submit_Radio - Fatal编程技术网

jQuery submit();没有增值

jQuery submit();没有增值,jquery,forms,button,submit,radio,Jquery,Forms,Button,Submit,Radio,我使用图像替换单选按钮,隐藏按钮并根据单击的图像选择它们 单击图像时,我希望他们选择一个单选按钮,然后提交 这是我的原始代码: $('#settings img').click(function() { $(this).parent().find(":radio").attr('checked', true); }); 如果我点击一张图片,然后提交,这是可行的 我扩展了它,以便在单击时提交表单: $('#settings img').click(function() { $(this)

我使用图像替换单选按钮,隐藏按钮并根据单击的图像选择它们

单击图像时,我希望他们选择一个单选按钮,然后提交

这是我的原始代码:

$('#settings img').click(function() {
  $(this).parent().find(":radio").attr('checked', true);
});
如果我点击一张图片,然后提交,这是可行的

我扩展了它,以便在单击时提交表单:

$('#settings img').click(function() {
  $(this).parent().find(":radio").attr('checked', true);
  $("#settings").submit();
});
在这种情况下,表单将提交,但不会更改值。如果我通过点击按钮提交它,它可以正常工作

我哪里做错了?

我已经把你的问题岔开了:并做了一些补充:

$('#settings').submit(function(){
    alert($(this).serialize());
});

$('#settings img').click(function() {
    $(this).parent().find(":radio").attr('checked', true);
    $("#settings").submit(); // submits form on click
});​
通过在单击处理程序之前添加提交处理程序并测试分叉提琴,我只能在我的浏览器(Mac OS X 10.8上的Safari 6.0.1)中验证它是否按预期工作(根据描述)

请确保:

  • 没有nester表单。浏览器可能会对这一点非常挑剔
  • 只有一个元素的id为“#设置”

  • 我唯一能想到的另一个可能的问题是你的浏览器。。。您在使用什么?

    Javascript是异步的。 看起来表单在jQuery选中复选框之前提交。尝试将提交放在回调中:

    $('#settings img').click(function() {
      $(this).parent().find(":radio").attr('checked', true).one('change', function(){
         $("#settings").submit();
      });
    });
    

    也许不是最好的答案,但如果你给你的图像一个类,你可以用.image替换:#settings img然后它就可以工作了

    这里的问题是给
    checked
    属性的
    true

    $('#settings img').click(function() {
      $(this).parent().find(":radio").attr('checked', 'checked');
    });
    
    所提供的在铬合金中工作。但它在Firefox中并不存在。要使其在Firefox中也能工作,请使用
    checked
    属性的
    checked

    $('#settings img').click(function() {
      $(this).parent().find(":radio").attr('checked', 'checked');
    });
    
    另一种方法是直接操作DOM元素的
    选中的
    属性:

    $(this).parent().find(":radio").get(0).checked = true;
    

    你的HTML看起来怎么样?谢谢这真是一个评论。。。不是答案。有点正确,但这样我可以跟踪问题,稍后再回答。这就是问题的目的。您将收到此帖子的任何更改通知。。。如果你真的想回答这个问题,请只发布一个答案。您在这里所做的只是问另一个问题。@Kris我们希望您使用此资源作为简单的参考,但我不会删除此答案。这是为了帮助OP,等待澄清。如果放弃,这只会是一个潜在的错误答案。我们有很多这样的人。为什么我真的要在这里帮助别人?!甚至最初的一行程序也包含两个可能非常有用的指针。我没有违反任何规则,我知道其他然后有点生气,因为人们判断没有理解或坦率地阅读。我本可以在评论中贴上一行,但因为它确实试图回答这个问题……啊,当然,你是对的。我相信添加.attr('checked',true).one('change',function(){..回调代码..});虽然javascript确实有一些异步功能,而且jquery中的许多东西都使用这些功能,但是我现在无法进行测试。我不相信
    attr()。这不是他的问题。(redid comment,因为autocorrect更正错误)奇怪的是,我甚至删除了所有其他JS代码,以查看它是否与某个东西冲突,但它仍然不起作用。上述问题是由
    checked
    属性的值引起的,该属性应该被
    选中,而不是
    true