当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - how to check if substring has a same value in same input using Jquery

问题描述:

I have a hidden input with different values. I want when checkbox is unchecked to remove-replace value.

<input id="test" type="hidden" name="test" value="4220752209,4220752209,4220752094,4220752094">

I Have a table in whose first column I have a checkbox.

Every time when I click I get the hidden value and store it in input like this

if ($("#test").val() == '') {

//this is my data aData.ordercode

$("#test").val(aData.ordercode);

} else {

$('#test').val($('#test').val() + "," + aData.ordercode);

}

Now I want when I unchecked from table to find and replace the same value if you see my value has same numbers i want when uncheck a checkbox to find and I have doing like this

 if ($('input[type=checkbox]').is(':checked')) {

$(this).prop('checked', true);

} else {

$(this).prop('checked', false);

if ($("#test").val().indexOf(aData.ordercode) > -1) {

alert("Yes");

}

网友答案:

Please try these code :-

<input id="checkbox" type="checkbox" value="4220752209"/> 
    <input id="test" type="hidden" name="test" value="4220752209,4220752219,4220752095,4220752096"/>
    $('input[type=checkbox]').on('click',function(){
       val = $(this).val();
       if($(this).is(':checked')) {
          var temp_array = $("#test").val().split(",");
          temp_array = jQuery.grep(temp_array, function(value) {
             return value != val;
          });
          $("#test").val(temp_array.join());

       }
       else {
         $("#test").val($("#test").val()+","+val);
       }
    });
分享给朋友:
您可能感兴趣的文章:
随机阅读: