简体   繁体   English

在隐藏字段数组中添加和删除值,而无需提交表单

[英]Add and Remove values in a hidden field array, without submitting form

I'm trying to manipulate the values of a hidden field, without submitting the form. 我试图在不提交表单的情况下操纵隐藏字段的值。 The closest I found is Add and Remove values in a hidden field array 我发现的最接近的是隐藏字段数组中的“添加”和“删除”值

Problem I have with this is that it submits the form. 我对此的问题是它提交了表单。 In my project I'm not ready to submit the form yet. 在我的项目中,我尚未准备好提交表单。 There are more questions to be asked. 还有更多问题要问。 Here's a JSFiddle of my problem . 这是我的问题的中庸之道

I feel like I'm missing something like a 'return false'. 我觉得我缺少诸如“返回假”之类的东西。

Thanks for the help. 谢谢您的帮助。

jQuery.fn.extend({
addToArray: function(value) {
    return this.filter(":input").val(function(i, v) {
       var arr = v.split(',');
       arr.push(value);
       return arr.join(',');
    }).end();
},
removeFromArray: function(value) {
    return this.filter(":input").val(function(i, v) {
       return $.grep(v.split(','), function(val) {  
                return val != value;
              }).join(',');
    }).end();
}
});

Solution is to preventdefault event. 解决方案是防止发生默认事件。

Here's the solution: 解决方法如下:

            event.preventDefault();

http://jsfiddle.net/4kbq0veq/ http://jsfiddle.net/4kbq0veq/

You can add return false; 您可以添加return false; in the onClick code: 在onClick代码中:

<button onclick='$("#myField").addToArray("21"); return false;'>

To prevent submission. 为了防止提交。

I think you should ask a separate question, but I'm diving into this anyway. 我认为您应该问一个单独的问题,但是无论如何我都在深入探讨。 I modified the HTML in the jFiddle . 在jFiddle中修改了HTML。 Since you're using jQuery I took advantage of the click event handler. 由于您使用的是jQuery,因此我利用了click事件处理程序。 Initially I created a single handler to see what code was required. 最初,我创建了一个处理程序来查看所需的代码。 It looked like this initially: 最初看起来像这样:

$( "#strength" ).click(function() {
  event.preventDefault();
  if ($( '#goals' ).val().indexOf(',strength') >= 0) {
    $( '#goals' ).val($( '#goals' ).val().replace(',strength', ''));
    $( this ).removeClass('button');
    $( this ).addClass('buttonSelected');
  } else {
    $( '#goals' ).val($( '#goals' ).val() + ',strength');
    $( this ).removeClass('buttonSelected');
    $( this ).addClass('button');
  }
});

That was an okay starting point to figure out the required code. 那是确定所需代码的一个好的起点。 However I didn't want to repeat that code for each of the buttons. 但是我不想为每个按钮重复该代码。 So I factored out the code that I knew could be reused for the other buttons. 因此,我考虑了我知道可用于其他按钮的代码。 So it looked like this: 所以看起来像这样:

$( "#strength" ).click(function() {
  event.preventDefault();
  toggle(',strength', this);
});

function toggle(str, thisObj) {
  if ($( '#goals' ).val().indexOf(str) >= 0) {
    console.log(str + ' is already selected');
    $( '#goals' ).val($( '#goals' ).val().replace(str, ''));
    $( thisObj ).removeClass('buttonSelected');
    $( thisObj ).addClass('button');
  } else {
    console.log(str + ' is about to be selected');
    $( '#goals' ).val($( '#goals' ).val() + str);
    $( thisObj ).removeClass('button');
    $( thisObj ).addClass('buttonSelected');
  }
}

Then it is pretty straightforward to handle the additional buttons with some repetitive code: 然后,使用一些重复的代码来处理其他按钮非常简单:

$( "#weight" ).click(function() {
    event.preventDefault();
  toggle(',weight', this);
});

$( "#tone" ).click(function() {
    event.preventDefault();
  toggle(',tone', this);
});

You may be able to decrease the repetition, but I'm tired and hungry. 您也许可以减少重复,但我又累又饿。 Hope this helps you out! 希望这可以帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM