将代码分配给特定表单

时间:2014-07-21 17:11:18

标签: jquery ajax

我正在尝试使用jquery在form1上提交一个变量,该变量最后会禁用此表单的提交按钮并显示消息"已成功发送"。

页面上有另一个表单,名为form2,它被分配了一个操作来将变量提交到另一个没有调用jjax的页面,但我认为我的按钮禁用代码与form2冲突,使得它无法将变量发送到指定的行动页面。

<script language="javascript">
/**
 * Disable submit button
 */
$(function(){
      $('input:submit', '#form1').click(function(){
            $(this).val('Sent Successfully...');
            $(this).attr('disabled', 'disabled');
      });
});
</script>

窗口2

<form id="form2" name="form2" method="post" action="search.php">
          <label for="textfield"></label>
          <input type="text" name="textfield" id="textfield" />
          <input type="submit" name="button2" id="button2" value="Submit" />
        </form>

4 个答案:

答案 0 :(得分:0)

是的,它会偏转,因为你使用了两个选择器,一个将禁用所有提交按钮,然后#form1。尝试将其更改为:

$(function(){
      $('#form1').click(function(){
            $(this).val('Sent Successfully...');
            $(this).attr('disabled', 'disabled');
      });
});

答案 1 :(得分:0)

不需要#form1。只需使用input:submit定位按钮即可。请记住,这也适用于表单2的提交按钮,即当您单击form2中的提交时,它将更改文本并禁用它。

$(function(){
      $('input:submit').click(function(){
            $(this).val('Sent Successfully...');
            $(this).attr('disabled', 'disabled');
      });
});

答案 2 :(得分:0)

由于您使用input:submit,因此将禁用该特定页面中的所有提交按钮。

而不是这个

$(function(){
  $('input:submit', '#form1').click(function(){
        $(this).val('Sent Successfully...');
        $(this).attr('disabled', 'disabled');
  });
});

尝试使用:

$(function(){
  $('#form1').on('click','input:submit',function(){
        $(this).val('Sent Successfully...');
        $(this).attr('disabled', 'disabled');
  });
});

答案 3 :(得分:0)

尝试使用submit事件,然后使用.find()方法选择相应的按钮:

  $( '#form1' ).on( 'submit', function() {
      $(this)
          .find( '#button1' )
          .val( 'Sent Successfully...' )
          .attr( 'disabled', 'disabled' );
  });