How can i get id or value from dropify input on clicking the remove button if there is a default value?

huangapple go评论55阅读模式
英文:

How can i get id or value from dropify input on clicking the remove button if there is a default value?

问题

<input type="file" name="galleryImages{{ $key }}[]" value="1" data-default-file="{{ asset('storage/services/' . $service->slug . '/gallery/' . $key . '/' . $imageItem) }}" class="dropify" data-id="{{ $key }}">
$(document).ready(function() {
    $('.dropify').dropify(); // initializing dropify

    // Handling click event on Remove button of dropify
    $('.dropify-clear').click(function(e) {
        e.preventDefault();
        console.log(e.target.value);
        alert('Remove Hit'); // Here you can manage your ajax request to delete file from the database.
    });
});
英文:
&lt;input type=&quot;file&quot; name=&quot;galleryImages{{ $key }}\[\]&quot;
value=&quot;1&quot;
data-default-file=&quot;{{ asset(&#39;storage/services/&#39; . $service-\&gt;slug . &#39;/gallery/&#39; . $key . &#39;/&#39; . $imageItem) }}&quot;
class=&quot;dropify&quot; data-id=&quot;{{ $key }}&quot;
\&gt;

For each input field, I want to get back the value of that clicked input.

How can i get id or value from dropify input on clicking the remove button if there is a default value?

   $(document).ready(function() {
        $(&#39;.dropify&#39;).dropify(); //initializing dropify

        //Handling click event on Remove button of dropify
        $(&#39;.dropify-clear&#39;).click(function(e) {
            e.preventDefault();
            console.log(e.target.value);
            alert(&#39;Remove Hit&#39;, ); //Here you can manage you ajax request to delete
            //file from database.
        });
    });

答案1

得分: 1

要获取单击删除按钮(dropify-clear按钮)的Dropify输入的ID或值,您可以使用以下代码:

$('.dropify-clear').click(function(e) {
    e.preventDefault();

    // 获取单击输入的默认值
    var defaultValue = $(this).siblings('input[type="file"]').attr('data-default-file');
    console.log(defaultValue);

    // 获取单击输入的值
    var inputValue = $(this).siblings('input[type="file"]').val();
    console.log(inputValue);

    // 获取单击输入的ID(data-id)
    var inputId = $(this).siblings('input[type="file"]').attr('data-id');
    console.log(inputId);

    alert('Remove Hit'); // 在此处,您可以管理代码以执行任何您想要的操作
});

在上面的代码中,$(this) 引用被单击的删除按钮。我们可以找到相应的Dropify输入元素,并使用.attr('data-default-file')检索其默认值。同样,可以使用.attr('data-id')获取输入元素的ID。

请注意,我们不能使用.val()函数获取dropify输入的默认值,因为默认值放在属性(data-default-file)中。

英文:

To get the ID or value of the Dropify input on clicking the remove button (dropify-clear button), you can use the following code:

$(&#39;.dropify-clear&#39;).click(function(e) {
      e.preventDefault();

      // Getting the default value of the clicked input
      var defaultValue = $(this).siblings(&#39;input[type=&quot;file&quot;]&#39;).attr(&#39;data-default-file&#39;);
      console.log(defaultValue);

      // Getting the value of the clicked input
      var inputValue = $(this).siblings(&#39;input[type=&quot;file&quot;]&#39;).val();
      console.log(inputValue);

      // Getting the ID (data-id) of the clicked input
      var inputId = $(this).siblings(&#39;input[type=&quot;file&quot;]&#39;).attr(&#39;data-id&#39;);
      console.log(inputId);

      alert(&#39;Remove Hit&#39;); // Here you can manage your code to perform anything what you want
  });

In the code above, $(this) refers to the clicked remove button. we can find the corresponding Dropify input element and retrieve its default value using .attr(&#39;data-default-file&#39;). Similarly, the ID of the input element can be obtained using .attr(&#39;data-id&#39;).

Please note that we cann't get the default value of the dropify input by .val() function because the default value is placed in the attribute (data-default-file)

huangapple
  • 本文由 发表于 2023年5月17日 11:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76268395.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定