【问题标题】:Issues with attaching on click function through Jquery通过 Jquery 附加点击功能的问题
【发布时间】:2014-01-10 16:16:05
【问题描述】:

当屏幕加载和更改其中一个文本框时,我有一些禁用的复选框,复选框会自动选中,我希望在表单提交期间将复选框值提交给我的支持 bean,因此我将 onclick 函数附加到通过 Jquery 提交按钮以在提交时启用复选框,但仍然没有提交值,但是如果我直接向提交按钮的 onclick 添加一个 javascript 函数,则值会被提交。

我的代码是这样的

 <input type="checkbox" id="chck_box1" class="enableCheckbox"/>
 <input type="submit" id="submitButton" onclick="enableCheckBox()"/>

 there are two ways i implemented
 **1.**    --- this enables the checkbox and values submitted
 function enableCheckbox(){    
   $('.enableCheckbox').each(function() {
      $(this).removeAttr('disabled');

   });
 }
 **2.** -- this enables the checkbox but the values not submitted
 $("#submitButton").on("click", function(){
     $('.enableCheckbox').each(function() {
         $(this).removeAttr('disabled');

   });
  });

谁能帮我理解提交按钮 onclick 和 jquery 的 on(submit) 之间的区别

谢谢

【问题讨论】:

  • 提示:$(this).prop('disabled', false);.
  • 能否也发布您的 HTML 表单标记?
  • 我确实尝试了该道具,但这也无济于事..问题是 removeAttr 或道具在提交时从 javascript 函数调用而不是动态绑定时起作用... 这个 enableCheckBox 函数有我上面帖子中的代码..

标签: jquery html jsf-1.2


【解决方案1】:

我认为

<input type="submit" id="submitButton" onclick="enableCheckBox()"/>

首先触发表单提交事件,但是

 $("#submitButton").on("click", function(){

在提交表单之前触发按钮点击,这就是区别。我认为正确的方法是订阅

$("Your_form_id).on("submit" , function() {

并添加

return false;

在必要时防止提交表单的函数末尾(例如:通过ajax左右发送所有表单变量)。

【讨论】:

  • 我之前试过 $("Your_form_id).submit() 没用,但这个 on() 有效。谢谢
【解决方案2】:

我认为您在 '.enableCheckbox' 中有错字,可能是 '.enableChec*k*box'

在我的示例中一切正常 HTML:

<input type="checkbox" class="enableCheckbox" disabled="true">
<input type="button" id="enablechk" value="Enable">

脚本:

$("#enablechk").click(function() { 
     $('.enableCheckbox').each(function() {
     $(this).removeAttr('disabled');
 });
});

jsfiddle:

http://jsfiddle.net/zx10tomcat/LZBfD/

提交按钮onclick和jquery的on(submit)的区别 在第一种情况下,它只是一个 ckick 事件,在第二种情况下 - 提交 - 不同的对象。你也可以写 return false;在 on(submit) 函数和表单结束时不会提交(并发送任何数据)。 希望对您有所帮助。

【讨论】:

  • 谢谢。即使我启用了它,但在提交期间值没有提交到我的 backingbean,但是当从 HTML onclick 函数调用时它被启用并提交
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-03
相关资源
最近更新 更多